列表的笛卡尔积

25

我有一个问题,它实际上是一个通用的编程问题,但我的实现是用Java进行的,所以我会提供相应的示例。

我有一个类,像这样:

public class Foo {
    LinkedHashMap<String, Vector<String>> dataStructure;

    public Foo(LinkedHashMap<String, Vector<String>> dataStructure) {
        this.dataStructure = dataStructure;
    }

    public String[][] allUniqueCombinations() {
        //this is what I need to do
    }
}

我需要从我的LinkedHashMap中生成一个嵌套数组,它表示LHM中所有值的每个唯一组合。例如,如果我的LHM看起来像这样(伪代码,但我认为你可以理解..):

{"foo" => ["1","2","3"], "bar" => ["3","2"], "baz" => ["5","6","7"]};

那么我的String[][]应该长成这样:

{
   {"foo","bar","baz"},
   {"1","3","5"},
   {"1","2","5"},
   {"1","3","6"},
   {"1","2","6"},
   {"1","3","7"},
   {"1","2","7"},
   {"2","3","5"},
   {"2","2","5"},
   {"2","3","6"},
   {"2","2","6"},
   {"2","3","7"},
   {"2","2","7"},
   {"3","3","5"},
   {"3","2","5"},
   {"3","3","6"},
   {"3","2","6"},
   {"3","3","7"},
   {"3","2","7"},
}

我想这些就是全部了,我手动创建了它们(显然),所以可能会漏掉一些,但我认为这说明了我的意图。每个集合的顺序并不重要,只要包含所有独特的组合即可。同时需要明确的是,您不知道LHM中有多少元素,也不知道每个后续Vector中有多少元素。我找到了符合要求的答案,可以获得单个数组中所有元素的每个唯一组合的情况,但没有任何完全符合这种情况的解决方案。

更新:我将类型更改为字符串,因为我的真实世界示例实际上是字符串。我之前尝试使用整数使示例更易读,但到目前为止我得到的答案不能很好地转换为字符串。所以,是的,它们是数字,但在我的实际情况中,它们将是字符串,除了使用此特定应用程序的人之外,其他人不太可能理解它们。因此,这只是一个抽象。


快速问题 - 为什么使用Vector?https://dev59.com/WnM_5IYBdhLWcg3waieJ - josh.trow
为什么所有长度为3的uniqueCombinations?输入中[3,2]的含义是什么? - Adam
@josh.trow 它是一个向量,因为它就是一个向量。我没有一个好的解释给你。我不是静态类型的忠实粉丝。 - Chris Drappier
@Adam,这些唯一的组合长度为三,因为在这种情况下dataStructure.size() == 3。如果dataStructure顶层有4个元素,则每个元素都将是4。 - Chris Drappier
Integer键在LHM中的意义是什么?组合数组的排序应该基于键的顺序还是LHM的顺序?(例如,如果我按照3,2,1的顺序添加键到LHM中,那么我应该使用相同的排序还是1,2,3)假设我有键1,2,4,6的映射...组合数组应该跳过3和5或使用一些特殊值(例如0,-1等)? - Kevin K
没关系,现在用String的例子更清楚了。 - Kevin K
12个回答

22
尝试像这样:
public static void generate(int[][] sets) {
    int solutions = 1;
    for(int i = 0; i < sets.length; solutions *= sets[i].length, i++);
    for(int i = 0; i < solutions; i++) {
        int j = 1;
        for(int[] set : sets) {
            System.out.print(set[(i/j)%set.length] + " ");
            j *= set.length;
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    generate(new int[][]{{1,2,3}, {3,2}, {5,6,7}});
}

将打印:

1 3 5
2 3 5
3 3 5
1 2 5
2 2 5
3 2 5
1 3 6
2 3 6
3 3 6
1 2 6
2 2 6
3 2 6
1 3 7
2 3 7
3 3 7
1 2 7
2 2 7
3 2 7

我已经根据(我相信)Knuth的TAOCP书籍之一实现了上述算法(在评论中,@chikitin有一个更具体的参考:它在Knuth的《计算机编程艺术》PRE FASCICLE 2A第7.2.1.1节“生成所有n-元组”中,Addison Wesley出版社)。请注意,我将数组命名为set,但它们当然不必包含唯一元素。我使用它的时间,它们确实包含唯一元素,因此得名。

编辑

这基本上是一对一的翻译:

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Vector;

public class Foo {

    private LinkedHashMap<String, Vector<String>> dataStructure;

    public Foo(LinkedHashMap<String, Vector<String>> dataStructure){
        this.dataStructure = dataStructure;
    }

    public String[][] allUniqueCombinations(){
        int n = dataStructure.keySet().size();
        int solutions = 1;

        for(Vector<String> vector : dataStructure.values()) {
            solutions *= vector.size();            
        }

        String[][] allCombinations = new String[solutions + 1][];
        allCombinations[0] = dataStructure.keySet().toArray(new String[n]);

        for(int i = 0; i < solutions; i++) {
            Vector<String> combination = new Vector<String>(n);
            int j = 1;
            for(Vector<String> vec : dataStructure.values()) {
                combination.add(vec.get((i/j)%vec.size()));
                j *= vec.size();
            }
            allCombinations[i + 1] = combination.toArray(new String[n]);
        }

        return allCombinations;
    }

    public static void main(String[] args) {
        LinkedHashMap<String, Vector<String>> data = new LinkedHashMap<String, Vector<String>>();
        data.put("foo", new Vector<String>(Arrays.asList("1", "2", "3")));
        data.put("bar", new Vector<String>(Arrays.asList("3", "2")));
        data.put("baz", new Vector<String>(Arrays.asList("5", "6", "7")));

        Foo foo = new Foo(data);

        for(String[] combination : foo.allUniqueCombinations()) {
            System.out.println(Arrays.toString(combination));            
        }
    }
}

如果你运行上面的类,将会打印出以下内容:
[foo, bar, baz]
[1, 3, 5]
[2, 3, 5]
[3, 3, 5]
[1, 2, 5]
[2, 2, 5]
[3, 2, 5]
[1, 3, 6]
[2, 3, 6]
[3, 3, 6]
[1, 2, 6]
[2, 2, 6]
[3, 2, 6]
[1, 3, 7]
[2, 3, 7]
[3, 3, 7]
[1, 2, 7]
[2, 2, 7]
[3, 2, 7]

让我再玩一会儿,我可以看到它的方向。逻辑似乎没问题,只需要花费一些力气来调整数据结构以使其适配。 - Chris Drappier
2
如何解释内部for循环中的set[(i/j)%set.length]部分以及随后的j *= vec.size();?还是这是一个需要知道的技巧? - Ketcomp
2
感谢您提供的解决方案。该解决方案可以在Knuth的《计算机程序设计艺术》的PRE FASCICLE 2A第7.2.1.1节中找到,该书由Addison Wesley出版。 - chikitin
@chikitin,你知道7.2.1.1中这个答案所基于的算法是哪一个吗?我很难将该部分描述的算法与答案中的算法匹配。虽然这是一个旧答案,但指出适用于此处的特定算法将使得答案更好。目前,内层循环中发生的情况很难理解。 - mahonya
当成对数等于3147776时,我会遇到这个错误。Python成功地创建了笛卡尔积,但是你提供的代码会出现以下错误: at java.lang.Double.valueOf(Double.java:519)``` 即使我将堆空间分配为10 GB,仍然会出现相同的错误。这意味着这段代码存在一些内存泄漏问题。 - foobar
显示剩余5条评论

4

Guava提供了一个实用方法,可以返回给定集合列表的笛卡尔积:Sets.cartesianProduct


4

我知道你需要答案的时间已经过去很久了,但我还是想指出可以在Java应用程序的某些部分中切换到Groovy,并编写一个包装类来匹配所需的接口。这种排列的Groovy代码如下:

myListOfLists.combinations()

自从我在Java应用程序中开始使用Groovy,编写速度更快而且调试/分析更有趣(咳咳...)


太棒了的建议! - medge

4
生成产品时采用延迟加载的方式,即仅在访问元组时才创建它,这种方法如何?
/**
* A random access view of tuples of a cartesian product of ArrayLists
*
* Orders tuples in the natural order of the cartesian product
*
* @param T the type for both the values and the stored tuples, ie. values of the cartesian factors are singletons
* While the type of input sets is List<T> with elements being treated as singletons
*
*/

abstract public class CartesianProductView<T> extends AbstractList<T> {

private final List<List<T>> factors;
private final int size;

/**
 * @param factors the length of the factors (ie. the elements of the factors argument) should not change,
 *  otherwise get may not return all tuples, or throw exceptions when trying to access the factors outside of range
 */
public CartesianProductView(List<List<T>> factors) {
    this.factors = new ArrayList<>(factors);
    Collections.reverse(this.factors);
    int acc = 1;
    for (Iterator<List<T>> iter = this.factors.iterator(); iter.hasNext(); ) {
        acc *= iter.next().size();
    }
    this.size = acc;
}

@Override
public T get(int index) {
    if (index < 0 || index >= size()) {
        throw new IndexOutOfBoundsException(String.format("index %d > size() %d", index, size()));
    }

    T acc = null;
    for (Iterator<List<T>> iter = factors.iterator(); iter.hasNext();) {
        List<T> set = iter.next();
        acc = makeTupleOrSingleton(set.get(index % set.size()), acc);
        index /= set.size();
    }
    return acc;
}

@Override
public int size() {
    return size;
}

private T makeTupleOrSingleton(T left, T right) {
    if (right == null) {
        return left;
    }
    return makeTuple(left, right);
}

/**
 *
 * @param left      a singleton of a value
 * @param right     a tuple of values taken from the cartesian product factors, with null representing the empty set
 * @return          the sum of left and right, with the value of left being put in front
 */
abstract protected T makeTuple(T left, T right);
}

并像这样使用它

final List<List<String>> l1 = new ArrayList<List<String>>() {{ add(singletonList("a")); add(singletonList("b")); add(singletonList("c")); }};
final List<List<String>> l2 = new ArrayList<List<String>>() {{ add(singletonList("X")); add(singletonList("Y")); }};
final List<List<String>> l3 = new ArrayList<List<String>>() {{ add(singletonList("1")); add(singletonList("2")); add(singletonList("3")); add(singletonList("4")); }};


List<List<List<String>>> in = new ArrayList<List<List<String>>>() {{ add(l1); add(l2); add(l3); }};

List<List<String>> a = new CartesianProductView<List<String>>(in) {

    @Override
    protected List<String> makeTuple(final List<String> left, final List<String> right) {
        return new ArrayList<String>() {{ add(left.get(0)); addAll(right); }};
    }

};

System.out.println(a);

结果如下:
[[a, X, 1], [a, X, 2], [a, X, 3], [a, X, 4], [a, Y, 1], [a, Y, 2], [a, Y, 3], [a, Y, 4], [b, X, 1], [b, X, 2], [b, X, 3], [b, X, 4], [b, Y, 1], [b, Y, 2], [b, Y, 3], [b, Y, 4], [c, X, 1], [c, X, 2], [c, X, 3], [c, X, 4], [c, Y, 1], [c, Y, 2], [c, Y, 3], [c, Y, 4]]

作为额外的奖励,您可以使用它将所有字符串连接起来:
final List<String> l1 = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
final List<String> l2 = new ArrayList<String>() {{ add("X"); add("Y"); }};
final List<String> l3 = new ArrayList<String>() {{ add("1"); add("2"); add("3"); add("4"); }};


List<List<String>> in = new ArrayList<List<String>>() {{ add(l1); add(l2); add(l3); }};

List<String> a = new CartesianProductView<String>(in) {

    @Override
    protected String makeTuple(String left, String right) {
        return String.format("%s%s", left, right);
    }

};

System.out.println(a);

结果如下:
[aX1, aX2, aX3, aX4, aY1, aY2, aY3, aY4, bX1, bX2, bX3, bX4, bY1, bY2, bY3, bY4, cX1, cX2, cX3, cX4, cY1, cY2, cY3, cY4]

3
请看下面的两个方法,它们完全符合你的要求。我编写它们是为了通用性,无论你的列表有多长或者映射中存在多少键,生成的组合都是正确的。
下面的代码是迭代的,基于Python的itertools.product()函数算法来计算列表列表的笛卡尔积。
public String[][] allUniqueCombinations() {

    List<String> labels = new ArrayList<String>();
    List<List<String>> lists = new ArrayList<List<String>>();

    for (Map.Entry<String, Vector<String>> entry : dataStructure.entrySet()) {
        labels.add(entry.getKey());
        lists.add(entry.getValue());
    }

    List<List<String>> combinations = product(lists);
    int m = combinations.size() + 1;
    int n = labels.size();
    String[][] answer = new String[m][n];

    for (int i = 0; i < n; i++)
        answer[0][i] = labels.get(i);
    for (int i = 1; i < m; i++)
        for (int j = 0; j < n; j++)
            answer[i][j] = combinations.get(i-1).get(j);

    return answer;

}

private List<List<String>> product(List<List<String>> lists) {

    List<List<String>> result = new ArrayList<List<String>>();
    result.add(new ArrayList<String>());

    for (List<String> e : lists) {
        List<List<String>> tmp1 = new ArrayList<List<String>>();
        for (List<String> x : result) {
            for (String y : e) {
                List<String> tmp2 = new ArrayList<String>(x);
                tmp2.add(y);
                tmp1.add(tmp2);
            }
        }
        result = tmp1;
    }

    return result;

}

我用问题中的示例对它们进行了测试:

LinkedHashMap<String, Vector<String>> sample = 
    new LinkedHashMap<String, Vector<String>>();

Vector<String> v1 = new Vector<String>();
v1.add("1"); v1.add("2"); v1.add("3");
Vector<String> v2 = new Vector<String>();
v2.add("3"); v2.add("2");
Vector<String> v3 = new Vector<String>();
v3.add("5"); v3.add("6"); v3.add("7");

sample.put("foo", v1);
sample.put("bar", v2);
sample.put("baz", v3);

Foo foo = new Foo(sample);
String[][] ans = foo.allUniqueCombinations();
for (String[] row : ans)
    System.out.println(Arrays.toString(row));

打印出的答案是预期的(尽管组合的顺序不同):
[foo, bar, baz]
[1, 3, 5]
[1, 3, 6]
[1, 3, 7]
[1, 2, 5]
[1, 2, 6]
[1, 2, 7]
[2, 3, 5]
[2, 3, 6]
[2, 3, 7]
[2, 2, 5]
[2, 2, 6]
[2, 2, 7]
[3, 3, 5]
[3, 3, 6]
[3, 3, 7]
[3, 2, 5]
[3, 2, 6]
[3, 2, 7]

Oscar,很难决定是否接受你或Bart的答案,我必须选择Bart,因为他先回答了,但是你的答案也非常好,谢谢! - Chris Drappier

2
你可以使用Functional Java的List monad非常容易地解决这个问题:Functional Java的List monad
import fj.data.List;

public class cartesian {
 public static void main(String[] args) {
  List<String>  foo = List.list("a", "b");
  List<Integer> bar = List.list(1,2,3);
  List<Float>   baz = List.list(0.2f,0.4f,0.3f);

  List<P3<String, Integer, Float>> 
  // the Cartesian product is assembled into a list of P3's
  result = foo.bind(bar, baz, P.<String, Integer, Float>p3()); 

  String out = Show.listShow(Show.p3Show(Show.stringShow, Show.intShow, Show.floatShow))
               .showS(result);
  System.out.println(out);
 }
}

2

1
一个由字符串向量组成的LinkedHashMap是有些棘手的。我花了很多时间将解决方案转换为使用它,但最终我并没有生成ArrayOfArrays,而是生成了List of List,并将最后一步留给读者自己完成。
import java.util.*;
/**
    CartesianProductLHM   
*/
public class CartesianProductLHM
{
    LinkedHashMap <String, Vector<String>> dataStructure;

    public CartesianProductLHM (final String[] data) {
        dataStructure = new LinkedHashMap <String, Vector<String>> ();
        for (String str : data)
        {
            String [] kv = str.split (":");
            String [] values = kv[1].split (","); 
            Vector <String> v = new Vector <String> ();
            for (String s: values) {
                v.add (s);
            //  System.out.print (s); 
            }
            // System.out.println ("\n---");
            dataStructure.put (kv[0], v);
        }
        // System.out.println ("    --- --- ---");
    }

    List <String> getCombiFor (final int i, final List <List <String>> livs) 
    {
        List <String> ls = new ArrayList <String> ();
        if (! livs.isEmpty ()) {
            List <String> vs = livs.remove (0); 
            int idx = i % vs.size (); 
            String elem = vs.get (idx);
            ls.add (elem);
            ls.addAll (getCombiFor (i / vs.size (), livs));
        }
        return ls;
    }

    List <String> getOuterCombiFor (int i, List <List <String>> coll) 
    {
        List <String> ls = new ArrayList <String> ();
        if (! coll.isEmpty ()) {
            List <List <String>> livs = new ArrayList <List <String>> ();
            for (List<String> li : coll) 
            {
                livs.add (li);
            }   
            ls.addAll (getCombiFor (i, livs));
        } 
        return ls;  
    }   

    public List <List <String>> allUniqueCombinations () {
        Collection <Vector <String>> li = dataStructure.values (); 
        List <List <String>> lls = new ArrayList <List <String>> ();
        for (Vector <String> vs : li) {
            List <String> l = new ArrayList <String> ();
            for (String s : vs) {
                l.add (s);
            }
            lls.add (l);
        }
        int count = 1;
        for (Vector <String> vec: li) {
            count *= vec.size ();
        }       
        List <List <String>> result = new ArrayList <List <String>> ();
        for (int i = 0; i < count; ++i) 
        {
            List <String> l = getOuterCombiFor (i, lls);
            result.add (l);
        }
        return result;  
    }

    public static void main (String args[])
    {
        String[] arr = {"foo:1,2,3", "bar:a,b", "baz:5,6,7"};
        CartesianProductLHM cp = new CartesianProductLHM (arr);
        List <List <String>> lls = cp.allUniqueCombinations ();
        for (List <String> ls : lls) 
        {
            for (String s : ls)
                System.out.print (s + "\t");
            System.out.println ();
        }
    }
}

好的 - 是的,我解析了一些测试数据。

主要思想是,你有一些列表(abc,12,defg,...),在位置0有3种可能性,在位置1有2种可能性,在位置3有4种可能性等等,到目前为止有3*2*4种组合。

从数字0到23,您可以使用模运算从每个子列表中选择,并将其余数除以前一个列表的大小和剩余列表递归地传递给该过程,直到没有列表为止。


1

这里有一个链接, 它是C#的,但我相信你可以处理它!


1
我来晚了,但我跟随Shiomi的链接并将函数翻译成了Java。结果是一个易于理解和遵循的算法(可能我有点慢,因为我很难理解Bart Kiers的解决方案)。
这是它的代码(密钥是int类型,替换为String应该很简单): 用法
    public void testProduct(){
        Map<Integer, List<String>> data =   new LinkedHashMap<Integer, List<String>>(){{                
            put(0, new ArrayList<String>(){{
                add("John"); add("Sarah");                      
            }});                
            put(1, new ArrayList<String>(){{
                add("Red"); add("Green"); add("Blue"); add("Orange");
            }});
            put(2, new ArrayList<String>(){{
                add("Apple"); add("Tomatoe"); add("Bananna");                   
            }});
    }};

        List<String[]> product =  GetCrossProduct(data);
        for(String[] o : product)
            System.out.println(Arrays.toString(o));

    }

结果

[John, Red, Apple]
[John, Red, Tomatoe]
[John, Red, Bananna]
[John, Green, Apple]
[John, Green, Tomatoe]
[John, Green, Bananna]
[John, Blue, Apple]
[John, Blue, Tomatoe]
[John, Blue, Bananna]
[John, Orange, Apple]
[John, Orange, Tomatoe]
[John, Orange, Bananna]
[Sarah, Red, Apple]
[Sarah, Red, Tomatoe]
[Sarah, Red, Bananna]
[Sarah, Green, Apple]
[Sarah, Green, Tomatoe]
[Sarah, Green, Bananna]
[Sarah, Blue, Apple]
[Sarah, Blue, Tomatoe]
[Sarah, Blue, Bananna]
[Sarah, Orange, Apple]
[Sarah, Orange, Tomatoe]
[Sarah, Orange, Bananna]

笛卡尔积函数

    public static List<String[]> GetCrossProduct(Map<Integer, List<String>> lists)
    {
        List<String[]> results = new ArrayList<String[]>();
        GetCrossProduct(results, lists, 0, new String[(lists.size())]);
        return results;
    }

    private void GetCrossProduct(List<String[]> results, Map<Integer, List<String>> lists, int depth, String[] current)
    {
        for (int i = 0; i < lists.get(depth).size(); i++)
        {
            current[depth] = lists.get(depth).get(i);            
            if (depth < lists.keySet().size() - 1)
                GetCrossProduct(results, lists, depth + 1, current);
            else{
                results.add(Arrays.copyOf(current,current.length));                
            }
        }
    }       

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