Dijkstra algorithm (iii) Java detailed

Source: Internet
Author: User

Introduction to Dijkstra algorithm

Dijkstra (Dijkstra) algorithm is a typical shortest path algorithm, which is used to compute the shortest path of a node to another node.
Its main feature is to extend to the outer layer (breadth-first search idea) with the starting point as the center, until the extension to the endpoint.

Basic ideas

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

In addition, the introduction of two sets S and U. The function of S is to record the vertex of the shortest path (and the corresponding shortest path length), and U is to record the vertex of the shortest path (and the distance from the vertex to the start s).

At the beginning, only the starting point in S s;u is the vertex except S, and the path of the vertex in U is "the path of the beginning S to the vertex". Then, find the shortest vertex of the path from U and add it to S, then update the path of the vertices and vertices in U. Then, find the shortest vertex of the path from U and add it to S, then update the path of the vertices and vertices in U. ... Repeat until all vertices are traversed.

Operation Steps

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

(2) select "Shortest vertex k" from U and add vertex k to S, and remove vertex k from U.

(3) update the distance from each vertex in U to the start S. The reason for updating the distance of the vertices in U is that K is the vertex of the shortest path, so that K can be used to update the distance of other vertices, for example, (s,v) distance may be greater than (S,K) + (K,V).

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

Simply looking at the above theory may be more difficult to understand, the following examples to illustrate the algorithm.

Dijkstra algorithm diagram

The above figure G4 as an example to perform an algorithmic demo of Dijkstra (starting with the 4th Vertex D).

Initial state : S is the set of vertices that have been computed for the shortest path, and U is the set of vertices that are not computed except for the shortest path!
Step 1th : Add vertex D to S.

At this point, 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.

Step 2nd : Add vertex c to S.

After the previous operation, the distance between vertex C to start D is the shortest in U, so adding C to S, and updating the distance of vertices in U. Take vertex f as an example, the distance from F to D is ∞, but after adding C to S, the distance F to D is 9= (f,c) + (c,d).

At this point, s={d (0), C (3)}, U={a (∞), B (d), E (4), F (9), G (∞)}.

Step 3rd : Add vertex e to S.

After the previous operation, you have the shortest distance from vertex e to start D, so add E to S and update the distance of the vertices in U. In the case of Vertex F, the distance from F to D is 9, but after adding E to S, the distance from F to D is 6= (f,e) + (e,d).

At this point, s={d (0), C (3), E (4)}, U={a (∞), B (d), F (6), G (12)}.

Step 4th : Add vertex f to S.

At this point, s={d (0), C (3), E (4), F (6)}, U={a (d), B (d), G (12)}.

Step 5th : Add vertex g to S.

At this point, s={d (0), C (3), E (4), F (6), G (A)}, U={a (d), B (13)}.

Step 6th : Add vertex b to S.

At this point, s={d (0), C (3), E (4), F (6), G (a), B (a), U={a (22)}.

Step 7th : Add Vertex A to S.

At this point, s={d (0), C (3), E (4), F (6), G (a), B (A), A (22)}.

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

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Code description for the Dijkstra algorithm

The "adjacency matrix" as an example of the Dijkstra algorithm, for the "adjacency table" to achieve the diagram in the following will give the corresponding source code.

1. Basic definition

public class MATRIXUDG {
    
    private int medgnum;        Number of sides
    private char[] Mvexs;       Vertex set
    private int[][] Mmatrix;    Adjacency matrix
    private static final int INF = Integer.max_value;   Maximum value
    
    ...
}

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

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.