Python Dijkstra 算法
我正在尝试编写 Dijkstra 算法,但是我正在努力解决如何在代码中“说出”某些事情。 为了可视化,这里是我想要使用数组表示的列:
max_nodes
A B C Length Predecessor Visited/Unvisited
A 0 1 2 -1 U
B 1 0 1 -1 U
C 2 1 0 -1 U
因此,将会有几个数组,如下面的代码所示:
def dijkstra (graph, start, end)
network[max_nodes][max_nodes]
state [max_nodes][length]
state2 [max_nodes][predecessor]
state3 [max_nodes][visited]
initialNode = 0
for nodes in graph:
D[max_nodes][length] = -1
P[max_nodes][predecessor] = ""
V[max_nodes][visited] = false
for l in graph:
length = lengthFromSource[node] + graph[node][l]
if length < lengthFromSourceNode[w]:
state[l][length] = x
state2[l][predecessor]
state3[l][visited] = true
x +=1
粗体部分是我陷入困境的地方 - 我正在尝试实现算法的这一部分:
3.对于当前节点,考虑其所有未访问的邻居并计算它们的暂定距离。例如,如果当前节点(A)的距离为6,并且连接它与另一个节点(B)的边为2,则通过A到B的距离将是6+2=8。如果此距离小于之前记录的距离,则覆盖该距离
4. 当我们考虑完当前节点的所有邻居后,将其标记为已访问。访问过的节点将不再被检查;现在记录的距离是最终的和最小的
我认为我走在正确的轨道上,我只是坚持如何说“从一个节点开始,获取从源到节点的长度,如果长度更小,覆盖之前的值,然后移动到下一个节点
I am trying to write Dijkstra's Algorithm, however I am struggling on how to 'say' certain things in code.
To visualize, here are the columns I want represented using arrays:
max_nodes
A B C Length Predecessor Visited/Unvisited
A 0 1 2 -1 U
B 1 0 1 -1 U
C 2 1 0 -1 U
So, there will be several arrays, as seen in my code below:
def dijkstra (graph, start, end)
network[max_nodes][max_nodes]
state [max_nodes][length]
state2 [max_nodes][predecessor]
state3 [max_nodes][visited]
initialNode = 0
for nodes in graph:
D[max_nodes][length] = -1
P[max_nodes][predecessor] = ""
V[max_nodes][visited] = false
for l in graph:
length = lengthFromSource[node] + graph[node][l]
if length < lengthFromSourceNode[w]:
state[l][length] = x
state2[l][predecessor]
state3[l][visited] = true
x +=1
The part in bold is where I am stuck on - I am trying to implement this section of the algorithm:
3. For current node, consider all its unvisited neighbors and calculate their tentative distance. For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance, overwrite the distance
4. When we are done considering all neighbors of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal
I think I am on the right track, i'm just stuck on how to say 'start at a node, get the length from source to a node, if length is smaller, overwrite previous value, then move to next node
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还使用了字典来存储网络。
数据格式如下:
source:{destination:cost}
创建网络字典(用户提供)
最短路径算法(用户需要指定起始和终止节点)
I also used a dictionary to store the network.
Data is in the following format:
source: {destination: cost}
create a network dictionary (user provided)
shortest path algorithm (user needs to specify start and terminal nodes)
首先,我认为这是一个家庭作业问题,因为最好的建议是不要自己编写它,而是在网络上找到现有的实现。 例如,这是一个看起来相当不错的。
假设您确实需要重新发明轮子,那里引用的代码使用字典来存储节点数据。因此,您可以向其提供类似以下内容:
这似乎是呈现图形信息的更直观的方式。访问过的节点和距离也可以保存在字典中。
First, I assume this is a homework problem, as the best suggest is to not bother writing it yourself, but to find an existing implementation on the web. Here's one that looks pretty good, for example.
Assuming you do need to reinvent the wheel, the code referenced there uses dictionaries to store the node data. So you feed it something like:
This seems a more intuitive way of presenting your graph information. Visited nodes and distances can be kept in dictionaries as well.