Java Dijkstra's shortest path algorithm to achieve

Dijkstra shortest path algorithm is a well known algorithm, the starting point is a single full-path algorithm. This algorithm is known as a "greedy algorithm" success story. This article will attempt the next most popular language to introduce this great algorithm, and give java implementation code.

First, knowledge ready

1, shows a data structure of FIG.

  There are various data structures used to store the graph, the algorithm used in the author is the adjacency matrix.

  FIG storage adjacency matrix is ​​represented by two arrays of FIG. A one-dimensional array vertex information memory map, a two-dimensional array (the adjacency matrix) stored arcs or edges in the graph of information.

Let G with n vertices, the adjacency matrix is ​​a square matrix of n * n, defined as:

img

  img

As can be seen from the above, the array edges undirected graph is a symmetric matrix. The so-called matrix of order n is a symmetric matrix element satisfies aij = aji. I.e., the axis from the top left to the main diagonal matrix lower right corner, bottom left corner and top right corner of the element corresponding to the element are all equal.

From this matrix, it is easy to know the information in the figure.

(1) To determine whether any vertex has two endless side is very easy;

(2) to know the degree of a vertex, in fact, the vertices vi and the adjacency matrix or elements of the i-th row (i-th column);

(3) Find all neighboring vertex vi is the matrix element in row i and scanned again, arc [i] [j] is adjacent to a point;

And while the degree of stress to the FIG., The degree of the vertex vi is one, exactly the number of i-th column and each of. The degree of vertex vi is 2, i.e. i-th row and the number of each.

  There is also similar to FIG defined, it is not repeated herein.

2, a single starting point for the full path

    The so-called single starting point for the full path, refers to a figure from a starting point of departure, the shortest path to all nodes.

3, the basic knowledge of graph theory (Readers are looking for relevant information on their own)

4, the complementary slackness condition

 Provided scalar d1, d2, ...., dN satisfy

    dj <= di + aij, (i, j) Shoku于 A,

 And P is ik i1 as a starting point for the end of the road, if

    dj = di + aij, all the edges of P (i, j)

 Holds, P is the shortest path from the i1 to ik. Wherein the complementary slackness condition is satisfied shortest path problem referred to above two formulas.

Second, the idea of ​​the algorithm

1, so G = (V, E) is a weighted undirected graph. G, if two adjacent nodes, i and j. of aij (In this and later expressed as subscripts, note) of node i to node j is the weight, in the algorithm may be understood as a distance. Each node has a value DI (node ​​flag) which represents the distance from the starting point of a route to it.

  2, V algorithm initial array for storing a list of unused access node, we tentatively called candidate list. Node 1 is selected as a starting node. Start, d1 = 0, node 1, node other di = infinity, V for all nodes.
After the initial conditions, and then start the iterative algorithm, until the stop when V is the empty set. In particular the following iterative steps:

   The minimum value of the node d di removed from the candidate list. (V in this embodiment the data structure employed to achieve minimum priority queue is dequeued, preferably used Fibonacci pairs, before the article has been introduced, a substantial performance tips). In respect to the starting node of each edge node is removed does not include the V, (i, j) belongs to A, if dj> di + aij (breach relaxed condition), then let

  dj = di + aij, (If j had been removed from the V, indicating that the minimum distance has been calculated, the calculation is not involved)

  Can be seen in operation in the algorithm works, d values ​​of the nodes are not growing monotonously

  The algorithm diagrammed below

Third, code implementation

//接受一个有向图的权重矩阵,和一个起点编号start(从0编号,顶点存在数组中)
    //返回一个int[] 数组,表示从start到它的最短路径长度  
    public static int[] Dijsktra(int[][]weight,int start){
        int length = weight.length;
        int[] shortPath = new int[length];//存放从start到各个点的最短距离
        shortPath[0] = 0;//start到他本身的距离最短为0
        String path[] = new String[length];//存放从start点到各点的最短路径的字符串表示
        for(int i=0;i<length;i++){
            path[i] = start+"->"+i;
        }
        int visited[] = new int[length];//标记当前该顶点的最短路径是否已经求出,1表示已经求出
        visited[0] = 1;//start点的最短距离已经求出
        for(int count = 1;count<length;count++){
            int k=-1;
            int dmin = Integer.MAX_VALUE;
            for(int i=0;i<length;i++){
                if(visited[i]==0 && weight[start][i]<dmin){
                    dmin = weight[start][i];
                    k=i;
                }
            }
            //选出一个距离start最近的未标记的顶点     将新选出的顶点标记为以求出最短路径,且到start的最短路径为dmin。
            shortPath[k] = dmin;
            visited[k] = 1;
            //以k为中间点,修正从start到未访问各点的距离
            for(int i=0;i<length;i++){
                if(visited[i]==0 && weight[start][k]+weight[k][i]<weight[start][i]){
                    weight[start][i] = weight[start][k]+weight[k][i];
                    path[i] = path[k]+"->"+i;
                }
            }
        }
        for(int i=0;i<length;i++){
            System.out.println("从"+start+"出发到"+i+"的最短路径为:"+path[i]+"="+shortPath[i]);
        }
        return shortPath;
         
    }

  This is achieved using the Dijkstra shortest path method.

Then you can declare a constant, for example:

static final int MAX = 10000;

Then build a adjacency matrix inside the main method, you can call this method.

public static void main(String[] args) {
        int[][] weight = {
                {0,3,2000,7,MAX},
                {3,0,4,2,MAX},
                {MAX,4,0,5,4},
                {7,2,5,0,6},
                {MAX,MAX,4,6,0}
                };
        int start = 0;
        int[] dijsktra = Dijsktra(weight,start);
    }

  Reference from: http://www.cnblogs.com/junyuhuang/p/4544747.html

Guess you like

Origin www.cnblogs.com/liyao0312/p/11600481.html