【问题标题】:How to find all path for the list?如何找到列表的所有路径?
【发布时间】:2019-03-16 08:48:11
【问题描述】:

我有一个这样的列表:

[[A], [B, C, D], [E, F], [G]]

对于 Java 代码初始化:

 List<List<String>> data = new ArrayList<>();
 data.add(Arrays.asList("A"));
 data.add(Arrays.asList("B", "C", "D"));
 data.add(Arrays.asList("E", "F"));
 data.add(Arrays.asList("G"));

并希望得到如下结果:

[[A,B,E,G],[A,B,F,G], [A,C,E,G],[A,C,F,G],[A,D,E,G],[A,D,F,G]]

怎么做?非常感谢。

【问题讨论】:

  • 你的尝试是什么?
  • 您正在寻找 java 中列表的排列。看这里stackoverflow.com/questions/16625220/…
  • @OmriAttiya 他不是在寻找所有排列。他正在寻找从一组节点开始的有向无环图的所有路径(在本例中仅在A)。

标签: java algorithm graph


【解决方案1】:

你可以写一个递归算法来解决这个问题。对于每个递归调用,该算法在图中向下移动一层。它的要点是首先计算当前所在层下的所有路径,然后将当前层中的所有节点添加到这些路径中。

这里有一些伪代码可以帮助你:

paths(input) {
    if input is empty -> return empty // This is your base case

    currentNodes = input[0]
    subPaths = paths(input.tail) // Recursive call with the rest of your input

    if subPaths is empty -> return input // The paths from the last layer is itself

    result = emptyList()
    for all nodes in currentNodes
        for all paths in subPaths
            prepend node to path and add to result

    return result
}

【讨论】:

    【解决方案2】:

    感谢@OmriAttiya。现在我给出答案。

    这是一个Cartesian Product 问题。

    如果你使用 guava lib,则一行

    List<List<String>> cartesian = Lists.cartesianProduct(operationTypeList);
    

    笛卡尔无递归

    public static List<List<String>> cartesianNoRecursive(List<List<String>> data) {
        int total = 1;
        for (int i = 0; i < data.size(); i++) {
            total *= data.get(i).size();
        }
        List<List<String>> result =  new ArrayList<>(total);
        for (int i = 0; i < total; i++) {
            result.add(new ArrayList<>());
        }
        int now = 1;
        // used times for every element in one loop
        int itemLoopNum = 1;
        // total  times of every element
        int loopPerItem = 1;
        for (int i = 0; i < data.size(); i++) {
            List<String> temp = data.get(i);
            now = now * temp.size();
            //index of target result
            int index = 0;
            int currentSize = temp.size();
            itemLoopNum = total / now;
            loopPerItem = total / (itemLoopNum * currentSize);
            // index of data
            int dataIndex = 0;
            for (int j = 0; j < temp.size(); j++) {
                for (int k = 0; k < loopPerItem; k++) {
                    if (dataIndex == temp.size()) {
                        dataIndex = 0;
                    }
                    for (int m = 0; m < itemLoopNum; m++) {
                        result.get(index).add(temp.get(dataIndex));
                        index++;
                    }
                    dataIndex++;
    
                }
            }
        }
        return result;
    }
    

    笛卡尔递归

    public static List<List<String>> cartesianRecursive(List<List<String>> list) {
        List<List<String>> result = new ArrayList<List<String>>();
        int numSets = list.size();
        String[] tmpResult = new String[numSets];
        cartesianRecursive(list, 0, tmpResult, result);
    
        return result;
    }
    
    public static void cartesianRecursive(List<List<String>> list, int n, String[] tmpResult, List<List<String>> result) {
        if (n == list.size()) {
            result.add(new ArrayList<String>(Arrays.asList(tmpResult)));
            return;
        }
    
        for (String i : list.get(n)) {
            tmpResult[n] = i;
            cartesianRecursive(list, n + 1, tmpResult, result);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多