Java - 根据数组密钥对查找所有可能的路径

时间:2017-07-12 07:47:38

标签: java recursion regression dijkstra

我有几个密钥对的数组,我想从一个元素中找到所有可能的路径并返回到它,例如:

array { 

a-b
a-c
a-d
b-a
b-c
b-s
d-c
c-a
c-d
c-a
d-a
....

}

我正在做一些foreach循环但是我被困在给定的数据集。有没有更好的方法呢?

这就是我所做的:

1)分开所有键

new array1 = {a,b,c,d,e,f,g......}

2)Foreach循环并找到它的直接路径,例如:

'a' => b , c , d , e ....
'b' => a , c, d, e

3)我被困在这里

a-b,现在来自b - 它可以采用许多不同的路径,我不知道如何针对所有不同的可能路径嵌套。

非常感谢任何帮助

我期待的是:

a-b-a
a-b-c-a
a-b-d-a
a-b-e-a
a-b-c-d-a
a-b-c-e-a
a-c-b-e-a
a-e-c-b-a
.......

3 个答案:

答案 0 :(得分:0)

You could make a loop which continues until all paths start and end with the same node. I.e. you start with a list of a single path containing only the starting node. For each of its neighbors you copy the path, append the neighbor, and add it to the list. Repeat until all paths in the list starts and ends with the same node.

You would also need some check to avoid infinite loops.

答案 1 :(得分:0)

最简单的解决方案是枚举给定节点对的所有可能路径。 我确定我忘记了一些情况,但这应该可以在一个遍历对的循环中实现+一些清理操作:

List<Path> paths = new ArrayList<>();
for (Pair p : pairs) {
    // add the path created by this pair
    paths.add(new Path(p));

    // find paths that contain this pair's starting edge and fork them
    List<Path> forkedPaths = new ArrayList<>();
    for (Path path : paths) {
        if (path.contains(p.getStart()) {
          // fork creates a new path: (a-b-c).fork(b-d) = (a-b-d)
          forkedPaths.add(path.fork(p));
        }
    }
    paths.addAll(forkedPaths);
}
removeDuplicates(paths);

在此之后,您应该有一个排序路径列表:

a-b-a
c-d
c-d-a

更容易检查您的预期状况。

这不是计算上最优的,但应该提供一个良好的起点。

答案 2 :(得分:0)

您可以查看此课程并进行测试。 这不是最干净的方式,但我认为这是一个开始。

public class TestRecursiveResearch {

    public String[] myList = {"a-b", "a-d", "b-a","b-c","d-c", "c-d"};

    public void search(){
        TreeSet<String> res = new TreeSet<String>();
        for (String pair : myList) {
            String[] splitted = pair.split("-");
            int findedCharFrom = (int)splitted[0].charAt(0);
            int findedCharTo = (int)splitted[1].charAt(0);

            String searchCombination = "";
            if(findedCharFrom < findedCharTo){
                for (int i = findedCharFrom+1 ; i <= findedCharTo; i ++) {
                    searchCombination += "" + (char) i;
                }
            }else{
                for (int i = findedCharFrom-1 ; i >= findedCharTo; i--) {
                    searchCombination += "" + (char) i;
                }
            }

            TreeSet<String> tmpRes = new TreeSet<String>();
            for (String sub : getAllSub(searchCombination)) {
                int n = sub.length();
                tmpRes.addAll(permute(sub, 0, n-1));
            }

            for (String string : tmpRes) {
                res.add((char)findedCharFrom +""+string+""+(char)findedCharFrom);
            }

        }
        for (String string : res) {
            System.out.println(string);
        }
    }

    private TreeSet<String> getAllSub(String str){
        TreeSet<String> allSubs = new TreeSet<String>();
        for(int i=0;i<str.length();i++){
            allSubs.add(str.substring(i));
            for(int j=i;j<str.length();j++){
                if(j>i){
                    allSubs.add(str.substring(i,j));
                    String res = str.substring(i,j-1) + str.substring(j,str.length());
                    if(!res.equals(""))
                        allSubs.add(res);
                }
            }
        }
        return allSubs;
    }
    private TreeSet<String> permute(String str, int l, int r)
    {
        TreeSet<String> res = new TreeSet<String>();
        if (l == r)
            res.add(str);
        else
        {
            for (int i = l; i <= r; i++)
            {
                str = swap(str,l,i);
                res.addAll(permute(str, l+1, r));
                str = swap(str,l,i);
            }
        }
        return res;
    }

    public String swap(String a, int i, int j)
    {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }


    public static void main(String[] args)
    {
        TestRecursiveResearch permutation = new TestRecursiveResearch();
        permutation.search();
    }
}

结果:

aba
abca
abcda
abda
abdca
aca
acba
acbda
acda
acdba
ada
adba
adbca
adca
adcba
bab
bcb
cdc
dcd