Dijkstra's Algorithm of shortest path

本文介绍了一种使用Java实现的迪杰斯特拉最短路径算法,并提供了完整的代码示例。该算法适用于带权有向图,通过递归计算从起点到每个顶点的最短距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you do graph, sooner or later you will reach this point, Dijkstra :) 

This Dutch computer scientist is/was more famous for his surprising quotes, and his huge amount of reading, while here we just quote his algorithm of shortest path in a directed and cyclic graph.

In the upper graph, what's the shortest path (distance) from vertex 1 to each vertex?

1->1 : 0 for sure;

1->2: 4? nope, 3 is correct;

1->3: 2 for sure;

1->4: 

1->5:

1->6: unreachable, marked with -1 would be ok.


so, here is my java solution to shortest path problems like this one:

https://github.com/breezedu/rosalind/blob/master/DijkstraAlgorithm.java


the readin txt document and output result document are also available:

https://github.com/breezedu/rosalind/blob/master/TXT/dijk.txt

and 

https://github.com/breezedu/rosalind/blob/master/TXT/dijk_output.txt


There are too many lectures online, but very few available questions/solutions with solid codes, so, feel free to check my codes.

in my codes, I did not use typical graph API, instead I used an arraylist to store all neighbor vertices of each vertex; 

then I built a matrix to store distance between two vertices, like the distance from v1 to v2 is distMatrix[v1][v2].

Create an array to mark whether vertex[n] have been visited or not: (vert[] array);

Create an array to indicate the potential shortest distance from initial vertex to each vertex, dist[] array, dist[1] = 0;


call dijkstra() method to recursively calculate the distance from initial vertex to each vertex.

by the end of dijkstra() method, mark current vertex as visited (change vert[i] = 0 to vert[i] = 1); 

check out the minimum distance from these un-visited vertices; set that vertex as the current vertex, call dijkstra() to calculate the newly assigned current vertex;


:) 


Dijkstra's Shortest Path Algorithm是一种用于解决带权图单源最短路径问题的贪心算法。下面通过一个简单的例子来解释Dijkstra's算法,并画出其图表。 假设我们有如下的带权图: ``` 2 3 (A)-----(B)-----(C) | | | |5 |4 |1 | | | (D)-----(E)-----(F) 7 6 ``` 其中,每个节点的名称用大写字母表示,括号中的数字表示节点之间的边权。 现在我们要从节点A出发,求到其它所有节点的最短距离。 Dijkstra's算法的流程如下: 1. 初始化,将起点A加入节点集合S中,并将起点到所有节点的距离初始化为正无穷。 ``` dist[A] = 0 dist[B] = inf dist[C] = inf dist[D] = inf dist[E] = inf dist[F] = inf S = {A} ``` 2. 按照距离起点的距离从小到大的顺序,依次将节点加入节点集合S中,并更新其它节点的距离。 在本例中,第一个被加入节点集合S中的节点是A,因此需要更新与A相邻的节点B和D的距离。通过从A到B和D的距离,发现从起点A到节点D的距离更短,因此更新dist[D]的值为7,并将节点D加入节点集合S中。 ``` dist[A] = 0 dist[B] = 2 dist[C] = inf dist[D] = 7 dist[E] = inf dist[F] = inf S = {A, D} ``` 接下来,选择距离起点最近的节点B,并更新与其相邻的节点E的距离。通过从A到B再到E的距离,发现从起点A到节点E的距离更短,因此更新dist[E]的值为9,并将节点E加入节点集合S中。 ``` dist[A] = 0 dist[B] = 2 dist[C] = inf dist[D] = 7 dist[E] = 9 dist[F] = inf S = {A, D, E} ``` 然后,选择距离起点最近的节点D,并更新与其相邻的节点B和E的距离。通过从A到D再到B和E的距离,发现不需要更新任何节点的距离。 ``` dist[A] = 0 dist[B] = 2 dist[C] = inf dist[D] = 7 dist[E] = 9 dist[F] = inf S = {A, D, E, B} ``` 接下来,选择距离起点最近的节点C,并更新与其相邻的节点B的距离。通过从A到B再到C的距离,发现从起点A到节点C的距离更短,因此更新dist[C]的值为5,并将节点C加入节点集合S中。 ``` dist[A] = 0 dist[B] = 2 dist[C] = 5 dist[D] = 7 dist[E] = 9 dist[F] = inf S = {A, D, E, B, C} ``` 然后,选择距离起点最近的节点F,并更新与其相邻的节点E的距离。通过从A到B再到C再到F的距离,发现从起点A到节点F的距离更短,因此更新dist[F]的值为11,并将节点F加入节点集合S中。 ``` dist[A] = 0 dist[B] = 2 dist[C] = 5 dist[D] = 7 dist[E] = 9 dist[F] = 11 S = {A, D, E, B, C, F} ``` 此时,所有节点都已经加入节点集合S中,算法结束。最终得到的起点A到其它节点的最短距离如下所示: ``` dist[A] = 0 dist[B] = 2 dist[C] = 5 dist[D] = 7 dist[E] = 9 dist[F] = 11 ``` 在图表中,用红色标记出了每次选择的节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值