Reference from: Http://my.oschina.net/luckid/blog/382615?fromerr=jJ44fzFI
As shown in the figure
The adjacency matrix is as shown in the figure:
The code is as follows:
/** * Created by Huluo on 2016/2/16.
* * * * * single-source vertex shortest path problem solution: * Shortest Path problem: Given the weighted direction of the graph G and source point V0, the shortest distance from v0 to other vertices in the graph is obtained.
* Dijkstra algorithm: Calculates the shortest distance from a point to the rest of the points by increasing the path length. * * public class Dijkstra {/* Max: Maximum value given, indicating unreachable between vertices * dist[]: An array storing the shortest path length * prve[]: Storing the precursor vertex of the current vertex *
A[][]: The adjacency matrix for a given test/private static int max = Integer.max_value;
private static int dist[] = new INT[6];
private static int prve[] = new INT[6]; private static int a[][] = {{0, max, X, Max, n}, {max, 0, 5, Max, Max, Max}, {max, max, 0, Max, Max}, {
Max, Max, Max, 0, Max, N, Max, Max, Max, 0, and {max, Max, Max, Max, Max, 0}};
Dijkstra algorithm public void Dijkstra (int v, int[][] A, int dist[], int prve[]) {int n = dist.length-1;
S[]: The store has found the vertex of the shortest path, false is not boolean[] s = new Boolean[n + 1];
for (int i = 1; I <= n; i++) {//Initialize dist[] array dist[i] = A[v][i];
S[i] = false; * * pRve[] Array stores the shortest path between the source point and Vertex VI, the precursor vertex of the vertex, * if unreachable from source to Vertex VI, the precursor vertex is-1 */if (Dist[i] < Integer.max_value)
Prve[i] = v;
else Prve[i] =-1; } Dist[v] = 0; Initialize V0 source point belongs to s set s[v] = true; Indicates that the shortest path for the V0 source point has been obtained for (int i = 1; I <= n; i++) {int temp = Integer.max_value;//temp staging V0 source point to V
The shortest path of the i vertex int u = v; for (int j = 1; J <= N; j +) {if ((!s[j]) && Dist[j] < temp) { Vertex VI does not belong to s set the current vertex does not belong to s set (shortest path is not obtained) and the distance v0 more near u = j; Update current source point, current VI as the source of the next path temp = Dist[j]; Update current Shortest Path}} S[u] = true; Vertex vi s SET/update current shortest path and path length for (int j = 0; J <= N; j +) {if (!s[j)
) && A[u][j] < Integer.max_value {//The current vertex does not belong to s set (shortest path is not evaluated) and the current vertex has a precursor vertex int newdist = Dist[u] + a[u][j]; Cumulative Update Shortest Path if (Newdist < dist[j]) {dist[j] = newdist; The shortest path after update prve[j] = u; Current vertex join precursor vertex set}}}//Result output method * * M: Source point * []p: Update knot The predecessor Vertex set * []d: The shortest path set after the update result/public void Outpath (int m, int[] p, int[] D) {for (int i = 0; i < dis T.length;
i++) {//The current vertex has been obtained the shortest path and the current vertex is not equal to the source point if (D[i] < integer.max_value && I!= m)
{System.out.print ("V" + i + "<--"); int next = P[i]; Sets the predecessor vertex of the current vertex while (next!= m) {//If the predecessor vertex is not one, the loop evaluates the remaining precursor vertex SYSTEM.OUT.PR
Int ("V" + Next + "<--");
Next = P[next];
} System.out.println ("V" + M + ":" + d[i]); }//The current vertex does not obtain the shortest path processing method else if (I!= m) System.out.println ("V" + i + "<--" + "V")
"+ M +": no path "); } public static void Main (string[] args) {//TODO auto-generated method stub Dijkstra d =
New Dijkstra ();
D.dijkstra (0, A, dist, prve);
D.outpath (0, Prve, Dist);
}
}
Reference from: Http://my.oschina.net/luckid/blog/382615?fromerr=jJ44fzFI