如何计算arraylist中的最小距离

时间:2014-11-01 04:08:14

标签: java arrays graph multidimensional-array adjacency-matrix

我有一个矩阵,矩阵中的距离是用欧氏距离计算的。 在那之后,我必须搜索对象之间的最小距离。但我必须将目标顶点保持为源顶点。我举了一个例子。

P1      P2      P3      P4      
0       3,0014  1,1245  1,1000  
3,0014  0       3,0571  2,5374  
1,1245  3,0571  0       1,0651  
1,1000  2,5374  1,0651  0   

基于对象P1的最小距离为:(P1,P4:1,1000),(P1,P3:1,1245),(P1,P2:3,0014) 但是我必须在对象P1中显示路径,路径应该是:(P1,P4),(P4,P3),(P3,P2) P1是源顶点,P4是目标顶点,P4应该是源顶点和ect。 我必须用java代码编写 这是我尝试的方法:

 public LinkedList<EdgeTest> getKruskalEdge(Double kDistance, int index) {
    PriorityQueue<EdgeTest> edge = new PriorityQueue<EdgeTest>();
    if (index == -1) {
        System.out.println("Kosong");
    } else {
        for (int i = 0; i < vertexList.length; i++) {

            if (adjMat[index][i] != -1 && adjMat[index][i] != 0) {
                if (adjMat[index][i] <= kDistance) {




                    edge.add(new EdgeTest(vertexList[index].toString(),
                            vertexList[i].toString(), adjMat[index][i]));


                }
            }


        }
        System.out.println("");
        while (!edge.isEmpty()) {

            insertEdge(edge.remove());

        }
    }
    return kruskalEdge;
}

0 个答案:

没有答案