The Dijkstra algorithm is a familiar one in the shortest path algorithm, and is a single-origin full-path algorithm. This algorithm is known as a successful model of "greedy algorithm". This article will then try to introduce this great algorithm in the most popular language and give Java implementation code.
First, knowledge preparation
1, the data structure of the representation diagram
There are many data structures used to store graphs, the adjacency matrix is used in this algorithm.
The adjacency matrix of a graph is stored in two arrays to represent the graph. A one-dimensional array stores the vertex information in a graph, and a two-dimensional array (adjacency matrix) stores information about edges or arcs in the diagram.
Figure G has n vertices, then the adjacency matrix is a n*n square, defined as:
As can be seen from the above, the edge array of the non-graph is a symmetric matrix. The so-called symmetric matrix is the element of n-order matrix satisfies AIJ = Aji. That is, from the upper-left corner of the matrix to the lower-right corner of the main diagonal axis, the upper-right corner of the element and the lower-left corner of the corresponding elements are all equal.
From this matrix, it is easy to know the information in the graph.
(1) It is easy to judge whether any two vertices have a boundless edge;
(2) To know the degree of a vertex is actually the sum of the elements of this vertex VI in the first row or (column i) of the adjacency matrix;
(3) Finding all the adjacency points of Vertex VI is to scan the element I of the matrix once, Arc[i][j] 1 is the adjacency point;
and the direction of the map to pay attention to the degree and the degree of the Vertex VI in the degree of 1, just is the sum of the number of the first column I. The dimension of vertex VI is 2, which is the sum of the number of rows I.
The definition of a map is similar, so do not repeat it.
2. Single starting point full path
The so-called single-origin full-path, refers to in a diagram, from a starting point, to all nodes of the shortest path.
3, the basic knowledge of graph theory (readers need to find relevant information on their own)
4. Complementary relaxation conditions
Set scalar d1,d2,...., DN to meet
Dj<=di + AIJ, (i,j) belongs to A,
and P is the path to the end of IK as the starting point of I1, if
dj = di + AIJ, on all sides of P (i, J)
Set, then p is the shortest path from I1 to IK. wherein, satisfies the above two type to be called the shortest path problem the complementary relaxation condition.
Second, the idea of arithmetic
1, make G = (v,e) for a weighted without the graph. If there are two adjacent nodes in G, I and J. AIJ (in this and behind are denoted as subscript, note) for node I to node J weights, in this algorithm can be understood as the distance. Each node has a value of Di (the node tag) that represents its distance from the starting point to one of its paths.
2, the algorithm initially has an array of V to store the list of non-access nodes, we are called a candidate list. The selected Node 1 is the starting node. At the beginning, Node 1 d1=0, the other nodes di= Infinity, and V is all nodes.
After the condition is initialized, the iteration algorithm is then started until V is set to stop when it is empty. The specific iteration steps are as follows:
Remove the node di with the lowest D value from the candidate list. (In this case, the data structure of V uses the priority queue to achieve the minimum value of the dequeue, preferably using Fibonacci pairs, in the previous article has been introduced, performance has a significant hint). For each edge that starts with the node, excluding the node to remove V, (i, J) belongs to A, if DJ > di + AIJ (breach of relaxation conditions),
dj = di + AIJ, (if J has been removed from V, indicating that its minimum distance has been calculated, not participating in this calculation)
You can see that the D value of the node is not increasing in the arithmetic engineering.
The specific algorithm is illustrated below
Third, the code implementation
Accepts a weighted matrix of a graph, and a starting point number start (numbered from 0, vertex exists in the array)//returns an array of int[] that represents the shortest path length from start to its public static int[] Dijsktra (int[][]weight,int start) {int length = weight.length;int[] ShortPath = new int[length];// The shortest distance from start to each point shortpath[0] = 0;//start to the shortest distance to him is 0String path[] = new string[length];//A string representation of the shortest path from the start point to each point for ( int i=0;i<length;i++) {Path[i] = start+ "," +i;} int visited[] = new int[length];//marks whether the shortest path to the current vertex has been calculated, 1 indicates that the shortest distance from the 1;//start point has been calculated 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;}} Select an unmarked vertex closest to start marks the newly selected vertex for the shortest path, and the shortest path to start is DMin. SHORTPATH[K] = dmin;visited[k] = 1;//with K as the middle point, fixed the distance from start to the point not visited 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];p ath[i] = path[k]+ "," +i;}}} for (int i=0;i<length;i++{System.out.println (the shortest path from "+start+" to "+i+" is: "+path[i]+" = "+shortpath[i]");} return ShortPath;}
This is the method of using the Dijkstra algorithm to realize the shortest path.
You can then declare a constant, for example:
static final int MAX = 10000;
Then build an adjacency matrix in the main method and 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
Java implementation of the shortest path Dijkstra algorithm