了解Dijkstra算法

编程入门 行业动态 更新时间:2024-10-18 21:17:08
本文介绍了了解Dijkstra算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图理解Dijkstra算法以找到最短路径。

我想到了这个示例,其中顶层表格与图片相对应在左下角。

现在,我的问题是我不理解从步骤1到步骤2的过渡:

在UX中,我们可以通过将X的成本加V来转换为UXV (即2)到当前成本(即1; UX成本)。因此,总和为3,但由于这个数字大于我已经发现的2,所以我们不会对其进行更改。在第一步中,我们有两个成本都相同的选项。 UXY和UXV,但是为什么算法选择使用UXY而不是UXV?

请先感谢!

解决方案

当您有两个或两个以上的选项费用相同时,则继续进行选择不会有任何区别。 / p>

在 Dijkstra的算法维基百科文章包含用于执行该算法的伪代码的部分。您会看到,在伪代码中,Q中有一行 u←顶点,其行距为min dist [u] ,这意味着您选择了成本最低的一个选项。当您有更多相同价格的选择时,只需选择其中任何一个。

对于您的具体示例,这意味着您也可以使用UXV而不是UXY。 可能导致更多步骤,但是算法完成时最终结果是相同的。

I'm trying to understand the Dijkstra Algorithm for finding the shortest path.

I've came up to this example, where the top table corresponds to the image in the bottom left corner.

Now, my problem is that I don't understand the transition from step 1 to step 2:

When we are in UX we can travel to UXV by adding the cost of X to V (which is 2) to our current cost (which is 1; the cost of UX). So the sum would be 3, but since this I bigger then the 2 we already found we don't change it. In step 1 we have two options which both have the same cost; UXY and UXV, but why does the algorithm choose to go to UXY instead of UXV?

Thanks in advance!

解决方案

When you have two or more options with the same cost, it does not make any difference with which option you proceed.

In the Dijkstra's algorithm Wikipedia article there is a section with a pseudocode for implementing the algorithm. You can see that in the pseudocode there is a line u ← vertex in Q with min dist[u], which means that you choose one option with the lowest costs. When you have more options with the same cost, you just take any of those.

For your concrete example, this means that you could also go to UXV instead of UXY. This might lead to more steps, but the end result is the same when the algorithm finishes.

更多推荐

了解Dijkstra算法

本文发布于:2023-11-29 16:56:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   Dijkstra

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!