Dijkstra 算法概念和编码问题
问题更新
我目前正在努力在 Python 中实现 Dijkstra 算法,我在这里查看了有关该算法的其他问题,但似乎没有一个适合我正在寻找的问题。
目前我的程序读取两个文本文件,其中一个包含网络路由图network.txt(基本上是路由成本)。
0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,0,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0
以及包含所需路由 (route.txt) 的
B>F
网络路由表: (每条线都是一个节点及其链接,因此节点 A 链接到 B、C、D 和 E)
A B C D E F G
A [0, 2, 4, 1, 6, 0, 0]
B [2, 0, 0, 0, 5, 0, 0]
C [4, 0, 0, 0, 0, 5, 0]
D [1, 0, 0, 0, 1, 1, 0]
E [6, 5, 0, 1, 0, 5, 5]
F [0, 0, 5, 1, 5, 0, 0]
G [0, 0, 0, 0, 5, 0, 0]
网络上的节点: ([Structure] PreviousNode, DistanceFromSource, Visited)
A [-1, 1000000000, False]
B [-1, 1000000000, False]
C [-1, 1000000000, False]
D [-1, 1000000000, False]
E [-1, 1000000000, False]
F [-1, 1000000000, False]
G [-1, 1000000000, False]
我有点理解 Dijkstra 算法,但是使用我拥有的两种数据结构,再加上必须用 Python 编写它,我不知道从这里去哪里,因为我无法在不懂语言的情况下,我要弄清楚如何做到这一点。
我有一些非常基本的“伪代码”来说明我下一步需要做什么
3 - 检查当前节点的所有未访问的邻居并计算 它们距起始节点的暂定距离,覆盖 如果新值较低,则现有距离
4 - 将当前节点标记为已访问(不会再次检查)
5 - 如果不在目的地且存在未访问节点,则转到最小> 的未访问节点。距初始节点的距离,使其成为当前节点,否则结束
6 - 返回 3
有谁能够帮助我将“伪代码”翻译成代码或者更有意义的伪代码会很棒,因为我正在努力解决这个问题
Question Updated
I am currently working on implementing Dijkstra's Algorithm in Python, I have looked at other questions here regarding the algorithm and none seem to fit what I am looking for.
Currently my program reads two text files, one containing a network routing diagram network.txt (basically the route costs).
0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,0,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0
and one containing the desired route (route.txt)
B>F
The Network Routing Table:
(Each Line Is a Node and Its Links so node A is linked to B, C, D and E)
A B C D E F G
A [0, 2, 4, 1, 6, 0, 0]
B [2, 0, 0, 0, 5, 0, 0]
C [4, 0, 0, 0, 0, 5, 0]
D [1, 0, 0, 0, 1, 1, 0]
E [6, 5, 0, 1, 0, 5, 5]
F [0, 0, 5, 1, 5, 0, 0]
G [0, 0, 0, 0, 5, 0, 0]
Nodes On The Network:
([Structure] PreviousNode, DistanceFromSource, Visited)
A [-1, 1000000000, False]
B [-1, 1000000000, False]
C [-1, 1000000000, False]
D [-1, 1000000000, False]
E [-1, 1000000000, False]
F [-1, 1000000000, False]
G [-1, 1000000000, False]
I kind of understand Dijkstra's Algorithm but using the two data structures I have, combined with having to write it in Python I don't have a clue where to go from here because I am unable to get my head round how to do this not knowing the language.
I have some very basic "pseudocode" to what I need to do next
3 - Examine all the unvisited neighbours of the current node and calculate
their tentative distances from the starting node, overwriting the
existing distance if the new value is lower4 - Mark current node as visited (will not be examined again)
5 - If not at destination and unvisited node exists go to the unvisited node with the smallest > distance from initial node and make it the current node, otherwise end
6 - Go back to 3
Is anyone able to assist me with translating that "pseudocode" into code or even more meaningful pseudocode would be great as I am struggling with this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在对象之间使用本机 python“链接”,例如:
无需为如此简单的问题创建自己的类。
观看此内容以获取灵感:
http://python.mirocommunity.org/视频/1530/pycon-2010-mastering-team-play
Use native python "links" between the objects, e.g.:
There's no need to create your own classes for such simple problem.
Watch this for inspiration:
http://python.mirocommunity.org/video/1530/pycon-2010-mastering-team-play