gpt4 book ai didi

Python Dijkstra 算法

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:25 25 4
gpt4 key购买 nike

我正在尝试编写 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. 当我们完成对当前节点的所有邻居的考虑后,将其标记为已访问。不会再次检查已访问的节点;它现在记录的距离是最终的和最小的

我认为我在正确的轨道上,我只是停留在如何说“从一个节点开始,获取从源到节点的长度,如果长度更小,覆盖以前的值,然后移动到下一个节点”

最佳答案

我还用字典来存储网络。数据采用以下格式:

来源:{destination: cost}

创建网络字典(用户提供)

net = {'0':{'1':100, '2':300},
'1':{'3':500, '4':500, '5':100},
'2':{'4':100, '5':100},
'3':{'5':20},
'4':{'5':20},
'5':{}
}

最短路径算法(用户需要指定起始节点和终止节点)

def dijkstra(net, s, t):
# sanity check
if s == t:
return "The start and terminal nodes are the same. Minimum distance is 0."
if s not in net: # python2: if net.has_key(s)==False:
return "There is no start node called " + str(s) + "."
if t not in net: # python2: if net.has_key(t)==False:
return "There is no terminal node called " + str(t) + "."
# create a labels dictionary
labels={}
# record whether a label was updated
order={}
# populate an initial labels dictionary
for i in net.keys():
if i == s: labels[i] = 0 # shortest distance form s to s is 0
else: labels[i] = float("inf") # initial labels are infinity
from copy import copy
drop1 = copy(labels) # used for looping
## begin algorithm
while len(drop1) > 0:
# find the key with the lowest label
minNode = min(drop1, key = drop1.get) #minNode is the node with the smallest label
# update labels for nodes that are connected to minNode
for i in net[minNode]:
if labels[i] > (labels[minNode] + net[minNode][i]):
labels[i] = labels[minNode] + net[minNode][i]
drop1[i] = labels[minNode] + net[minNode][i]
order[i] = minNode
del drop1[minNode] # once a node has been visited, it's excluded from drop1
## end algorithm
# print shortest path
temp = copy(t)
rpath = []
path = []
while 1:
rpath.append(temp)
if temp in order: temp = order[temp] #if order.has_key(temp): temp = order[temp]
else: return "There is no path from " + str(s) + " to " + str(t) + "."
if temp == s:
rpath.append(temp)
break
for j in range(len(rpath)-1,-1,-1):
path.append(rpath[j])
return "The shortest path from " + s + " to " + t + " is " + str(path) + ". Minimum distance is " + str(labels[t]) + "."

# Given a large random network find the shortest path from '0' to '5'
print dijkstra(net, s='0', t='5')

关于Python Dijkstra 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com