解决最短路径的Dijkstra算法详解,附加Java代码

1. 最短路径问题

最短路径问题是生活中经常碰到的一类问题,如机器人路径规划,数学竞赛以及真实的工程施工问题;甚至是我们程序员笔试必刷算法题。其实问题很简单,就是有很多个节点,我们要计算出一个初始点到各个节点的最短距离(或者得到最短路径的表示),如下图,怎么找出A点到其它各个点的最短路径及距离? 人眼扫描得到的结果:A到B、C、D……G距离为 [4, 14, 7, 7, 10, 12],人眼对付7个点还是绰绰有余的,什么?你扫描的结果和我不一样?那咱们还是分析一下原理,然后写个代码来处理一下。

2. Dijkstra算法 

2.1 基本原理:有一个数组记录了各个节点到初始节点的距离,该数组初始值是无穷大,然后如果选择A点作为起始点,那么修改数组中A到A的距离为0,并且更新那些与A直接连接的节点值,并把A标记为已访问,接下来,从距离数组中选择距离最小值,也就是B点,循环...

假设我们是要计算其它各点和A点的最短距离

  1. 我们选取A点作为出发点,那么很明显,我们只可以从和A点有直接联系的B、E点中选择一个最短路径点。先取出距离A点路径长度为4的B点,B点确定,那么我们记录B点以及B点和A的距离为{B(4)}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是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]; 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); } } ``` 这里是一个简单的示例,你可以根据自己的需求和数据结构进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值