2つのノード間の最短距離を求めます

フランクリンMemet:

私が発見し、私が作成していたことをグラフにダイクストラのアルゴリズムを実装 - 私の地元エリアのマップを示しています ここでは、画像の説明を入力します。

コードは、罰金に動作しますが、私はそれはそれはソースノードから位置ノードに到達するために訪問したことを、すべてのノードを表示したいです。そう例えば:私は1のソースノード(Banstead)、及び4に位置・ノード(Whteleafe)を設定した場合 - それは恐らくアレイでは、アレイ= {1,2,4}の任意のような訪問先のノードを格納しますアイデア?私が訪問したノードの値を格納する必要があり、そうするために - 私は、FXMLファイルでこれを入れて、楕円などのノードを持っているし、線でそれらを接続します。

package dijkstras;

public class Dijkstras {

    static class createGraph{
        int vertices;
        int matrix[][];

        public createGraph(int vertex){

            this.vertices = vertex;
            matrix = new int[vertex][vertex];
        }

        public void edge(int source, int destination, int weight){

            matrix[source][destination] = weight;
            matrix[destination][source] = weight;
        }

        int getMinVertex(boolean [] mst, int [] key){
            int minKey = Integer.MAX_VALUE;
            int vertex = -1;
            for (int i = 1; i < vertices; i++) {
                if(mst[i]==false && minKey>key[i]){
                    minKey = key[i];
                    vertex =i;
                }
            }

            return vertex;  
        }

        public void dijkstras(int sourceVertex){
            boolean[] spt = new boolean[vertices];
            int [] distance = new int[vertices];
            int infinity = Integer.MAX_VALUE;

            //setting all distances to infinity
            for(int i=1; i<vertices; i++){
                distance[i] = infinity;
            }

            //test for starting vertext = 1
            distance[sourceVertex] = 1;

            //create tree
            for(int i=1; i<vertices; i++){

                int vertexU = getMinVertex(spt, distance);

                spt[vertexU] = true;   
                //go through all possible paths adjacent to vertex
                for(int vertexV=1; vertexV<vertices; vertexV++){
                    //check if edge between Vu and Vv exists
                    if(matrix[vertexU][vertexV]>0){

                        //checks vertexV exists and if distance is not infinite
                        if(spt[vertexV]==false && matrix[vertexU][vertexV]!=infinity){

                            int newKey = matrix[vertexU][vertexV] + distance[vertexU];
                            if(newKey<distance[vertexV])
                                distance[vertexV] = newKey;
                        }
                    }
                }
            }

            System.out.println();      
            printDijkstras(sourceVertex, distance);
        }

        public void printDijkstras(int sourceVertex, int [] key){
            System.out.println("Dijkstra Algorithm:");
            int LocationOfChosenUser = 10;  
            System.out.println("Source Vertex: "+ sourceVertex + " to " +
              LocationOfChosenUser + " distance: " + (key[LocationOfChosenUser]-1));
        }
    }

    public static void graph() {
       int vertices = 18;
       createGraph graph = new createGraph(vertices);
       int sourceVertex = 8;
       //adding all nodes
       graph.edge(1,2,4);   graph.edge(1,17,3); 
       graph.edge(2,1,4);   graph.edge(2,4,4);   graph.edge(2,10,5); 
       graph.edge(3,4,1);   graph.edge(3,6,5);   graph.edge(3,5,2);
       graph.edge(4,2,4);   graph.edge(4,3,1);   graph.edge(4,5,2); 
       graph.edge(5,4,2);   graph.edge(5,3,2);   graph.edge(5,6,3);  graph.edge(5,9,4);  graph.edge(5,10,5); 
       graph.edge(6,3,5);   graph.edge(6,5,3);   graph.edge(6,7,2);  graph.edge(6,9,2);
       graph.edge(7,6,2);   graph.edge(7,8,5);   
       graph.edge(8,7,5);   graph.edge(8,9,4);   graph.edge(8,12,5);
       graph.edge(9,5,4);   graph.edge(9,8,4);   graph.edge(9,11,5); graph.edge(9,6,2); 
       graph.edge(10,2,5);  graph.edge(10,5,5);  graph.edge(10,13,1);graph.edge(10,14,3); graph.edge(10,17,6);
       graph.edge(11,9,5);  graph.edge(11,12,3); graph.edge(11,13,3);
       graph.edge(12,8,5);  graph.edge(12,11,3); graph.edge(12,15,4);
       graph.edge(13,11,3); graph.edge(13,10,1); graph.edge(13,14,2);
       graph.edge(14,10,3); graph.edge(14,13,2); graph.edge(14,16,6); graph.edge(14,15,6);
       graph.edge(15,12,4); graph.edge(15,14,5); graph.edge(15,16,9); 
       graph.edge(16,15,9); graph.edge(16,17,1); graph.edge(16,14,6);
       graph.edge(17,1,3);  graph.edge(17,16,1);

       graph.dijkstras(sourceVertex);
    }
    public static void main(String[] args){
        graph();
    }
}

あなたが見ることができるように私は10に8〜sourceVertexとLocationVertex(LocationOfChosenUser)を設定し、出力は次のようになります。

 Dijkstra Algorithm:
Source Vertex: 8 to 10 distance: 12
デビッド・バックボーン:

これを行うための最も簡単な方法は、単に、各ノードの前任者を追跡することです。あなたはエンド・ノードに到達したら、あなたは、あなたがどこから来たのかを見つけるために後戻りすることができます。

初期化を追加します。

int [] comeFrom = new int[vertices];

変化する

if(newKey<distance[vertexV])
    distance[vertexV] = newKey;

if(newKey<distance[vertexV]) {
    distance[vertexV] = newKey;
    comeFrom[vertexV] = vertexU;
}

そしてプリントアウトするとき

List<Integer> path = new ArrayList();
int pos = LocationOfChosenUser;

while(pos != sourceVertex) {
   path.add(pos);
   pos = comeFrom[pos];
}

for (int i=path.size()-1; i>=0; i--) {
   System.out.print(path.get(i));
   if (i > 0) System.out.print(" -> ");
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=192634&siteId=1