java - 组合算法

标签 java algorithm combinations

我在 java 中设计了一个算法,我生成一个列表元素的所有组合。例如,元素 [A, B, C] 它生成组合,[A][B][C ] , [AB] , [BC] , [ABC] 。或者根据元素 [A, A, B] 生成组合 [A] , [B] , [AA] , [AB] , [AAB] 。 这是我生成组合的代码。

private List<Elemento> combinazioneMassima = new ArrayList<>();
    private Log logger = LogFactory.getLog(Combinazioni3.class);

    public Combinazioni3(List<Elemento> generaCombinazioneMassima) {
        this.combinazioneMassima = generaCombinazioneMassima;
    }

    public void combine() {
        this.findAllCombinations(combinazioneMassima);
    }

    private static class Node{
        int lastIndex = 0;
    List<Elemento> currentList;
    public Node(int lastIndex, List<Elemento> list) {
            this.lastIndex = lastIndex;
            this.currentList = list;
    }
    public Node(Node n) {
            this.lastIndex = n.lastIndex;
            this.currentList = new ArrayList<Elemento>(n.currentList);
    }
    }

    public void findAllCombinations(List<Elemento> combinazioni) {
        Date dataInizio = new Date();
        List<List<Elemento>> resultList = new ArrayList<List<Elemento>>();
        LinkedList<Node> queue = new LinkedList<Node>();
        int n = combinazioni.size();
        ArrayList<Elemento> temp = new ArrayList<Elemento>();
        temp.add(combinazioni.get(0));
        queue.add(new Node(0, temp));
        // add all different integers to the queue once.
        for(int i=1;i<n;++i) {
                if(combinazioni.get(i-1) == combinazioni.get(i)) continue;
                temp = new ArrayList<Elemento>();
                temp.add(combinazioni.get(i));
                queue.add(new Node(i, temp));
        }
        // do bfs until we have no elements
        while(!queue.isEmpty()) {
                Node node = queue.remove();
                if(node.lastIndex+1 < n) {
                        Node newNode = new Node(node);
                        newNode.lastIndex = node.lastIndex+1;
                        newNode.currentList.add(combinazioni.get(node.lastIndex+1));
                        queue.add(newNode);
                }
                for(int i=node.lastIndex+2;i<n;++i) {
                        if(combinazioni.get(i-1) == combinazioni.get(i)) continue;
                        // create a copy and add extra integer
                        Node newNode = new Node(node);
                        newNode.lastIndex = i;
                        newNode.currentList.add(combinazioni.get(i));
                        queue.add(newNode);
                }
                GestoreRegole gestoreRegole = new GestoreRegole();
                gestoreRegole.esegui(node.currentList);
        }

    }

但是对于 input > 250 程序停止并且需要很长时间才能完成所有组合。 我怎样才能改进这个解决方案?或者有更好的解决方案吗?

最佳答案

对于input=250,会有这么多的组合: 看这个例子:

(1) {a}     => {a}
(3) {a,b}   => {a}, {b}, {a,b}
(7) {a,b,c} => {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c}

如你所见,会有2^n-1个元素

因此对于输入=250 - 2^250-1 = large number (1.8*10^75)

使用了过多的内存。我觉得20号左右(1048575)也会出事

关于java - 组合算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269037/

相关文章:

r - 将具有三列(日期、参数、结果)的矩阵解析为在指定日期 R 的每个参数都有一列的矩阵

performance - 组合(n 选择 k)并行化和效率

javascript - 使用高效算法对数组中的相同对进行计数

java - 不重复的组合

java - 小程序未找到关键事件或获取焦点

java - Java.util包中Arrays的排序算法

java - 如何在java中随机打开迷宫线

Python列表操作: Given a list of ranges number,返回组合范围的列表

java - 从主类调用参数到另一个文件中的另一个文件(1个包)

java - 为什么这个程序会抛出 java.lang.UnsupportedOperationException