使用二维数组的 Dijkstra 算法

发布于 2024-11-05 22:55:48 字数 357 浏览 4 评论 0原文

在过去的几天里,我一直在尝试实现这个算法。到目前为止,我已经成功创建了一个动态二维数组并插入节点之间的距离,一个删除节点之间路径的函数以及一个告诉我两个节点之间是否存在路径的函数。 现在我想实现一个函数,返回从节点 A 到节点 B 的最短路径。我知道 dijkstras 算法是如何工作的,并且我已经阅读了 wiki 上的伪代码,但无法自己编写任何代码。我真的被困在这里了。

我一直在思考代码应该是什么样子以及应该发生什么,这就是为什么我创建了这个函数来告诉我两个节点之间是否存在路径。我是否需要更多帮助功能来使 dijkstras 的实施变得更容易?

目前我只有 3 个节点,但我想要编写的代码通常需要在 n 个节点上工作。

任何形式的帮助表示赞赏。

For the past few days I've tried to implement this algorithm. This far I've managed to make a dynamic 2d array and insert the distances between nodes, a function to remove a path between nodes and a function that tells me if there is a path between two nodes.
Now I would like to implement a function that returns the shortest path from node A to node B. I know how dijkstras algorithm works and I've read the pseudo code on wiki without being able to write any code my self. I'm really stuck here.

I've been thinking about how the code should look like and what should happen thats why I've made that function that tells me if theres a path between two nodes. Do I need any more help functions which would make implementing of dijkstras easier?

For now I have only 3 nodes but the code I would like to write needs to work in general for n nodes.

Any kind of help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她说她爱他 2024-11-12 22:55:48

你可能想得太多了。

你需要两件事。您理解的干净的图形结构。对您理解的算法的良好描述。
如果你两者都有的话。开始编写一些代码。所需的帮助者将在途中变得显而易见。

-- 编辑 --
您可能需要以下一些数据结构
std::vector
std::list
std::priority_queue

You are probably thinking to much.

You need 2 things. A clean graph structure you understand. A good description of the algorithm you understand.
If you have both. Just start writing some code. Helpers needed will become obvious on the way.

-- edit --
You will probably need some of the following datastructures
std::vector
std::list
std::priority_queue

寻找一个思念的角度 2024-11-12 22:55:48

我找到了这个算法的几个代码,但也许最简单的一个更好,以便更好地理解它,所以你可以检查你的代码和这个算法之间的差异,并完成你的代码。按照自己的方式编程总是更好。

看看这个,看看是否有帮助。

http://vinodcse.wordpress。 com/2006/05/19/code-for-dijkstras-algorithm-in-c-2/

祝你好运。

I found several codes for this algorithm, but maybe it is better the simplest one in order to undertand it better, so you can check the differences between yours and this one, and complete yours. It is always better to program your way.

Have a look at this one and see if it helps.

http://vinodcse.wordpress.com/2006/05/19/code-for-dijkstras-algorithm-in-c-2/

Good luck.

雪若未夕 2024-11-12 22:55:48

编辑:代码已删除,我将给出提示:

  1. 将图存储为每个顶点的邻接列表的列表。 (类似这样的向量<向量<对>>>g(n);
  2. 使用一些数据结构来跟踪当前距离最小的顶点是什么状态。 (可能设置,或priority_queue具有O(m log(n))复杂度)
  3. 每次获取high_priority顶点(当前距离最小的顶点),将其从data_struct中删除并更新相邻到删除的距离一个顶点。

注意:如果您还想获得最小路径,请保留一些 vector。 previous 以及每次更新顶点距离(例如 v)时,设置 previous[v] = 来自此处的顶点索引。您的路径是 last, prev[last], prev[prev[last]],...,first 以相反的顺序。

Edit: Code deleted, and I'm going to give hints:

  1. Store graph as list of adjacency lists of each vertex. (something like this vector < vector < pair<int,int> > > g (n);)
  2. Use some data-structure to keep track what is the vertex with minimal distance in current state. (maybe set, or priority_queue to have O(m log(n)) complexity)
  3. Each time take high_priority vertex (vertex with minimal current distance), delete it from your data_structure and update distances of adjacent to deleted one vertexes.

Note: If you want to get minimal path as well, then keep some vector<int> previous and each time when updating distance of vertex (say v) set previous[v] = index of vertex from where you came here. Your path is last, prev[last], prev[prev[last]],...,first in reversed order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文