1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package com.nanzx.dijkstra;
import java.util.Arrays;
public class DijkstraAlgorithm {
public static void main(String[] args) { char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; int[][] matrix = new int[vertex.length][vertex.length]; final int N = 65535; matrix[0] = new int[] { N, 5, 7, N, N, N, 2 }; matrix[1] = new int[] { 5, N, N, 9, N, N, 3 }; matrix[2] = new int[] { 7, N, N, N, 8, N, N }; matrix[3] = new int[] { N, 9, N, N, N, 4, N }; matrix[4] = new int[] { N, N, 8, N, N, 5, 4 }; matrix[5] = new int[] { N, N, N, 4, 5, N, 6 }; matrix[6] = new int[] { 2, 3, N, N, 4, 6, N };
Graph graph = new Graph(vertex, matrix); Dijkstra djs = new Dijkstra(graph, 6); djs.dijkstra(); djs.show(); } }
class Graph { public char[] vertex; public int[][] matrix;
public Graph(char[] vertex, int[][] matrix) { this.vertex = vertex; this.matrix = matrix; }
public void showGraph() { for (int[] link : matrix) { System.out.println(Arrays.toString(link)); } } }
class Dijkstra { private int startVertex; private Graph graph; private int[] visited; private int[] prev; private int[] dist;
public Dijkstra(Graph graph, int startVertex) { this.startVertex = startVertex; this.graph = graph; this.visited = new int[graph.vertex.length]; this.prev = new int[graph.vertex.length]; this.dist = new int[graph.vertex.length];
visited[startVertex] = 1;
Arrays.fill(dist, 65535); for (int i = 0; i < graph.matrix[startVertex].length; i++) { if (graph.matrix[startVertex][i] < dist[i]) { dist[i] = graph.matrix[startVertex][i]; } } dist[startVertex] = 0;
for (int i = 0; i < prev.length; i++) { if (i != startVertex) { prev[i] = startVertex; } } prev[startVertex] = -1; }
public void dijkstra() { for (int n = 0; n < graph.vertex.length - 1; n++) { int min = 0; int minDist = 65535; for (int i = 0; i < dist.length; i++) { if (dist[i] < minDist && dist[i] != 0 && visited[i] != 1) { min = i; minDist = dist[min]; } } visited[min] = 1; for (int i = 0; i < graph.matrix[min].length; i++) { if (graph.matrix[min][i] + dist[min] < dist[i] && visited[i] == 0) { dist[i] = graph.matrix[min][i] + dist[min]; prev[i] = min; } } } }
public void show() {
System.out.print("visited数组:"); for (int i : visited) { System.out.print(i + " "); } System.out.println(); System.out.print("prev数组:"); for (int i : prev) { System.out.print(i + " "); } System.out.println(); System.out.print("dist数组:"); for (int i : dist) { System.out.print(i + " "); } System.out.println();
char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; System.out.print(vertex[startVertex] + " 到各个顶点的最短距离:"); int count = 0; for (int i : dist) { if (i != 65535) { System.out.print(vertex[count] + "(" + i + ") "); } else { System.out.println(vertex[count] + "(N) "); } count++; } } }
|