Java details of the Dijkstra algorithm

Source: Internet
Author: User

Source Quote: http://www.cnblogs.com/skywang12345/p/3711516.html Dijkstra Algorithm Introduction

The Dijkstra (Dijkstra) algorithm is a typical shortest path algorithm used to calculate the shortest path of a node to another node.
Its main feature is to extend from the center of the starting point to the outer layer (breadth-first search idea) until it expands to the end point.


Basic ideas

When you calculate the shortest path in Figure G by Dijkstra, you need to specify the starting point s (which is calculated from the vertex s).

In addition, two sets of S and U are introduced. The function of S is to record the vertices (and the corresponding shortest path lengths) that have been found for the shortest path, while U is the vertex (and the distance from the vertex to the start) of the shortest path that the record has not yet obtained.

Initially, only the starting point in S s;u is a vertex other than S, and the path of the vertex in U is the "Origin s to the path of the vertex". Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. ... Repeat the operation until all vertices are traversed.


Operation Steps

(1) Initially, S contains only the starting point S;u contains the other vertices except s, and the distance of the vertices in U is "the distance from the beginning s to that vertex" [For example, the distance of Vertex v in U is (s,v), and then S and V are not adjacent, then V is the distance of ∞].

(2) Select the shortest vertex k from U and add the vertex k to S, and remove the vertex k from U.

(3) update the distance from each vertex in U to the beginning S. The distance from the vertex in U is updated because the last step determines that K is the vertex of the shortest path, so that the distance from the other vertices can be updated by using K, for example, (S,V) may be greater than (S,K) + (k,v) distance.

(4) Repeat steps (2) and (3) until all vertices are traversed.

Simple to see the above theory may be more difficult to understand, the following examples to illustrate the algorithm.

Dijkstra algorithm Diagram

Take G4 as an example to perform an algorithm demonstration of Dijkstra (starting with the 4th Vertex D).

Initial state : S is the collection of vertices that have been calculated for the shortest path, and U is a collection of vertices that are not counted except for the shortest path!
1th Step : Add vertex D to S.
At this time, S={d (0)}, u={a (∞), B (∞), C (3), E (4), F (∞), G (∞)}. Note: C (3) indicates that the distance from C to start D is 3.

2nd Step : Add vertex c to S.
After the previous operation, the distance from vertex c to start D in U is the shortest, so C is added to S, and the distance from the vertex in U is updated. In the case of Vertex F, the distance from the previous F to D is ∞, but after C is added to S, the distance from F to D is 9= (f,c) + (c,d).
At this time, s={d (0), C (3)}, U={a (∞), B (+), E (4), F (9), G (∞)}.

3rd Step : Add vertex e to S.
After the previous operation, the distance from vertex e to start D in U is the shortest, so add e to S and update the distance of the vertices in U. In the case of Vertex F, the distance before F to D is 9, but after E is added to S, the distance from F to D is 6= (f,e) + (e,d).
At this time, s={d (0), C (3), E (4)}, U={a (∞), B (+), F (6), G (12)}.

4th Step : Add vertex f to S.
At this time, s={d (0), C (3), E (4), F (6)}, U={a (a), B (d), G (12)}.

5th Step : Add vertex g to S.
At this time, s={d (0), C (3), E (4), F (6), G (d)}, U={a (a), B (13)}.

6th Step : Add vertex b to S.
At this time, s={d (0), C (3), E (4), F (6), G (d), B (+)}, U={a (22)}.

7th Step : Add Vertex A to S.
At this time, s={d (0), C (3), E (4), F (6), G (a), B (d), A (22)}.

At this point, the shortest distance from the beginning D to each vertex is calculated:a (A) B (+) C (3) D (0) E (4) F (6) G (a).

code description for the Dijkstra algorithm

Taking "adjacency Matrix" as an example to illustrate the Dijkstra algorithm, the "adjacency table" Implementation of the graph in the following will give the corresponding source code.

1. Basic definition

public class MatrixUDG {    private int mEdgNum;        // 边的数量    private char[] mVexs;       // 顶点集合    private int[][] mMatrix;    // 邻接矩阵    private static final int INF = Integer.MAX_VALUE;   // 最大值    ...}

MATRIXUDG are the corresponding structures of the adjacency matrices. Mvexs is used to save vertices, medgnum is used to hold the number of edges, and Mmatrix is a two-dimensional array to hold the matrix information. For example, mmatrix[i][j]=1 means "vertex I (i.e. mvexs[i])" and "Vertex J (i.e. Mvexs[j])" are adjacency points; mmatrix[i][j]=0, they are not adjacency points.

2. Dijkstra algorithm

/* * Dijkstra Shortest path. * That is, the shortest path of "Vertex vs" to each of the other vertices in the chart. * * Parameter Description: * VS--starting vertex (start vertex). That is, the shortest path of vertex vs to other vertices is computed. * Prev--the predecessor vertex array. That is, Prev[i] is the value of "Vertex vs" to "vertex i" in all vertices experienced by the shortest path, at the vertex before "vertex i". * Dist--length array. That is, Dist[i] is the length of the shortest path of "Vertex vs" to "vertex i". */public void Dijkstra (int vs, int[] prev, int[] dist) {//flag[i]=true indicates that the shortest path to vertex vs to Vertex I has successfully obtained boolean[] flag = NE    W Boolean[mvexs.length];          Initialize for (int i = 0; i < mvexs.length; i++) {flag[i] = false;        The shortest path to vertex i is not yet available.              Prev[i] = 0;        The predecessor vertex of vertex i is 0.  Dist[i] = Mmatrix[vs][i];    The shortest path to vertex i is the right of "Vertex vs" to "vertex i".    }//Initialize "Vertex vs" itself flag[vs] = true;    Dist[vs] = 0;    Traverse mvexs.length-1 times, and find the shortest path of a vertex at a time.    int k=0;        for (int i = 1; i < mvexs.length; i++) {//search for the current smallest path;//i.e., in vertices that do not get the shortest path, find the closest vertex (k) to vs.        int min = INF; for (int j = 0; J < Mvexs.length; J + +) {if (Flag[j]==false && dist[j]<min) {min              = Dist[j];  K = J;        }}//Mark "Vertex K" for already acquired to shortest path flag[k] = true;        Fix the current shortest path and precursor vertex//i.e., update the shortest path and precursor vertex for vertices that do not get the shortest path, when the shortest path to vertex K is already. for (int j = 0; J < Mvexs.length; J + +) {int tmp = (mmatrix[k][j]==inf?            INF: (min + mmatrix[k][j]));                if (Flag[j]==false && (Tmp<dist[j])) {dist[j] = tmp;            PREV[J] = k;    }}}//Print the result of the Dijkstra shortest path System.out.printf ("Dijkstra (%c): \ n", Mvexs[vs]); for (int i=0; i < mvexs.length; i++) System.out.printf ("Shortest (%c,%c) =%d\n", Mvexs[vs], mvexs[i], dist[i]); }

Java details of the Dijkstra algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.