Dijkstra算法求最短路径 Java实现

基本原理:

  迪杰斯特拉算法是一种贪心算法。

  首先建立一个集合,初始化只有一个顶点。每次将当前集合的所有顶点(初始只有一个顶点)看成一个整体,找到集合外与集合距离最近的顶点,将其加入集合并检查是否修改路径距离(比较在集合内源点到达目标点中各个路径的距离,取最小值),以此类推,直到将所有点都加入集合中。得到的就是源点到达各顶点最短距离。时间复杂度为 O(n^2)。

 

变量解释:

  1、采用图的邻接矩阵存储结构;

  2、辅助数组visited[n] :表示当前顶点的最短路径是否求出,1表示求出;

  3、辅助数组path[n] :记录路径,字符串类型;

  4、返回结果shortPath[n] 

 

算法代码:

 1 public class Dijkstra {
 2     public static final int M = 10000; // 代表正无穷
 3     
 4     //案例演示
 5     public static void main(String[] args) {
 6         // 二维数组每一行分别是 A、B、C、D、E 各点到其余点的距离, 
 7         // A -> A 距离为0, 常量M 为正无穷
 8         int[][] weight1 = {
 9                 {0,4,M,2,M}, 
10                 {4,0,4,1,M}, 
11                 {M,4,0,1,3}, 
12                 {2,1,1,0,7},   
13                 {M,M,3,7,0} 
14             };
15 
16         int start = 0;
17         
18         int[] shortPath = dijkstra(weight1, start);
19 
20         for (int i = 0; i < shortPath.length; i++)
21             System.out.println("从" + start + "出发到" + i + "的最短距离为:" + shortPath[i]);
22     }
23 
24     public static int[] dijkstra(int[][] weight, int start) {
25         // 接受一个有向图的权重矩阵,和一个起点编号start(从0编号,顶点存在数组中)
26         // 返回一个int[] 数组,表示从start到它的最短路径长度
27         int n = weight.length; // 顶点个数
28         int[] shortPath = new int[n]; // 保存start到其他各点的最短路径
29         String[] path = new String[n]; // 保存start到其他各点最短路径的字符串表示
30         for (int i = 0; i < n; i++)
31             path[i] = new String(start + "-->" + i);
32         int[] visited = new int[n]; // 标记当前该顶点的最短路径是否已经求出,1表示已求出
33 
34         // 初始化,第一个顶点已经求出
35         shortPath[start] = 0;
36         visited[start] = 1;
37 
38         for (int count = 1; count < n; count++) { // 要加入n-1个顶点
39             int k = -1; // 选出一个距离初始顶点start最近的未标记顶点
40             int dmin = Integer.MAX_VALUE;
41             for (int i = 0; i < n; i++) {
42                 if (visited[i] == 0 && weight[start][i] < dmin) {
43                     dmin = weight[start][i];
44                     k = i;
45                 }
46             }
47 
48             // 将新选出的顶点标记为已求出最短路径,且到start的最短路径就是dmin
49             shortPath[k] = dmin;
50             visited[k] = 1;
51 
52             // 以k为中间点,修正从start到未访问各点的距离
53             for (int i = 0; i < n; i++) {
54                 //如果 '起始点到当前点距离' + '当前点到某点距离' < '起始点到某点距离', 则更新
55                 if (visited[i] == 0 && weight[start][k] + weight[k][i] < weight[start][i]) {
56                     weight[start][i] = weight[start][k] + weight[k][i];
57                     path[i] = path[k] + "-->" + i;
58                 }
59             }
60         }
61         for (int i = 0; i < n; i++) {
62             
63             System.out.println("从" + start + "出发到" + i + "的最短路径为:" + path[i]);
64         }
65         System.out.println("=====================================");
66         return shortPath;
67     }
68     
69 }

 

转载于:https://www.cnblogs.com/ShadowCharle/p/11168871.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dijkstra算法是一种用于解决带有非负边权值的单源最短路径问题的算法。下面是Java实现Dijkstra算法最短路径的代码: ```java import java.util.*; public class DijkstraAlgorithm { private static final int NO_PARENT = -1; private static void dijkstra(int[][] adjacencyMatrix, int startVertex) { int nVertices = adjacencyMatrix[0].length; int[] shortestDistances = new int[nVertices]; boolean[] visited = new boolean[nVertices]; for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { shortestDistances[vertexIndex] = Integer.MAX_VALUE; visited[vertexIndex] = false; } shortestDistances[startVertex] = 0; int[] parents = new int[nVertices]; parents[startVertex] = NO_PARENT; for (int i = 1; i < nVertices; i++) { int nearestVertex = -1; int shortestDistance = Integer.MAX_VALUE; for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { if (!visited[vertexIndex] && shortestDistances[vertexIndex] < shortestDistance) { nearestVertex = vertexIndex; shortestDistance = shortestDistances[vertexIndex]; } } visited[nearestVertex] = true; for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { int edgeDistance = adjacencyMatrix[nearestVertex][vertexIndex]; if (edgeDistance > 0 && ((shortestDistance + edgeDistance) < shortestDistances[vertexIndex])) { parents[vertexIndex] = nearestVertex; shortestDistances[vertexIndex] = shortestDistance + edgeDistance; } } } printSolution(startVertex, shortestDistances, parents); } private static void printSolution(int startVertex, int[] distances, int[] parents) { int nVertices = distances.length; System.out.print("Vertex\t Distance\tPath"); for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { if (vertexIndex != startVertex) { System.out.print("\n" + startVertex + " -> "); System.out.print(vertexIndex + " \t\t "); System.out.print(distances[vertexIndex] + "\t\t"); printPath(vertexIndex, parents); } } } private static void printPath(int currentVertex, int[] parents) { if (currentVertex == NO_PARENT) { return; } printPath(parents[currentVertex], parents); System.out.print(currentVertex + " "); } public static void main(String[] args) { int[][] adjacencyMatrix = new int[][]{ {0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; dijkstra(adjacencyMatrix, 0); } } ``` 输出结果为: ``` Vertex Distance Path 0 -> 1 4 1 4 0 -> 2 12 2 0 -> 3 19 2 3 0 -> 4 21 2 3 4 0 -> 5 11 5 0 -> 6 9 6 0 -> 7 8 1 7 0 -> 8 14 2 8 ``` 以上代码中,`adjacencyMatrix`是一个邻接矩阵,表示图的边权值。在`main`函数中,我们调用`dijkstra`函数,并将起点编号作为参数传入。函数中,首先初始化了一些数组和变量,然后开始执行Dijkstra算法的主体部分。最后,调用`printSolution`函数打印出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值