Python 我的dicts-of-dicts会为这个Dijkstra';s算法?
我对Python非常陌生,我首先尝试在这里实现Dijkstra的算法:。问题是我的矩阵如下所示:Python 我的dicts-of-dicts会为这个Dijkstra';s算法?,python,algorithm,graph,dijkstra,Python,Algorithm,Graph,Dijkstra,我对Python非常陌生,我首先尝试在这里实现Dijkstra的算法:。问题是我的矩阵如下所示: { 2845: {27026: {'weight': 0.05950338}, 83860: {'weight': 0.013386887}}, 12422: {27023: {'weight': 0.0787193}, 27026: {'weight': 0.041424256}, 59721: {'weight': 0.11553069}}, 27022:
{
2845: {27026: {'weight': 0.05950338}, 83860: {'weight': 0.013386887}},
12422: {27023: {'weight': 0.0787193}, 27026: {'weight': 0.041424256}, 59721: {'weight': 0.11553069}},
27022: {27025: {'weight': 0.1283993}, 83860: {'weight': 0.11746721}},
27023: {12422: {'weight': 0.0787193}, 27025: {'weight': 0.22683257}},
27025: {27022: {'weight': 0.1283993}, 27023: {'weight': 0.22683257}, 27026: {'weight': 0.20290035}},
27026: {2845: {'weight': 0.05950338}, 12422: {'weight': 0.041424256}, 27025: {'weight': 0.20290035}},
59721: {12422: {'weight': 0.11553069}},
83860: {2845: {'weight': 0.013386887}, 27022: {'weight': 0.11746721}}
}
这是否仍然适用于上述算法,或者我需要做一些轻微的调整,如果是这样,是什么
谢谢
编辑:
这里是我实现的算法:
def dijkstra(self, graph, start, end):
D = {} # Final distances dict
P = {} # Predecessor dict
for node in graph.keys():
D[node] = -1 # Vertices are unreachable
P[node] = ""
D[start] = 0 # The start vertex needs no move
unseen_nodes = graph.keys() # All nodes are unseen
while len(unseen_nodes) > 0:
shortest = None
node = ''
for temp_node in unseen_nodes:
if shortest == None:
shortest = D[temp_node]
node = temp_node
elif (D[temp_node] < shortest):
shortest = D[temp_node]
node = temp_node
unseen_nodes.remove(node)
for child_node, child_value in graph[node].items():
if D[child_node] < D[node] + child_value:
D[child_node] = D[node] + child_value
P[child_node] = node
path = []
node = end
while not (node == start):
if path.count(node) == 0:
path.insert(0, node) # Insert the predecessor of the current node
node = P[node] # The current node becomes its predecessor
else:
break
path.insert(0, start) # Finally, insert the start vertex
return path
我认为这应该是可行的,但这取决于你的代码。如果你想要一个更完整的答案,请张贴代码的其余部分
此外,与制作2D阵列相比,使用DICT可能更令人头痛。如果您真的想使用dict,我建议您使用。在给定的源代码示例中,权重只是一个整数,而不是dict。因为您的图形有一个带有“weight”键的dict,所以您必须根据该键更改代码 以下是代码的正确版本:
def dijkstra(self, graph, start, end):
D = {} # Final distances dict
P = {} # Predecessor dict
for node in graph.keys():
D[node] = -1 # Vertices are unreachable
P[node] = ""
D[start] = 0 # The start vertex needs no move
unseen_nodes = graph.keys() # All nodes are unseen
while len(unseen_nodes) > 0:
shortest = None
node = ''
for temp_node in unseen_nodes:
if shortest == None:
shortest = D[temp_node]
node = temp_node
elif (D[temp_node] < shortest):
shortest = D[temp_node]
node = temp_node
unseen_nodes.remove(node)
for child_node, child_value in graph[node].items():
if D[child_node] < D[node] + child_value['weight']: # I changed the code here
D[child_node] = D[node] + child_value['weight'] # I changed the code here
P[child_node] = node
path = []
node = end
while not (node == start):
if path.count(node) == 0:
path.insert(0, node) # Insert the predecessor of the current node
node = P[node] # The current node becomes its predecessor
else:
break
path.insert(0, start) # Finally, insert the start vertex
return path
def dijkstra(self、graph、start、end):
D={}#最终距离dict
P={}#前驱体dict
对于graph.keys()中的节点:
D[节点]=-1#顶点不可到达
P[节点]=“”
D[start]=0#起始顶点不需要移动
unseen_nodes=graph.keys()#所有节点都不可见
而len(不可见的_节点)>0:
最短=无
节点=“”
对于不可见节点中的临时节点:
如果最短==无:
最短=D[温度节点]
节点=临时节点
elif(D[temp_node]<最短):
最短=D[温度节点]
节点=临时节点
不可见的_节点。删除(节点)
对于子节点,在图[node]中指定子节点值。项()
如果D[child_node]
您链接的是一个python实现,为什么不试试呢?您是否将dijstra代码放入了类中?(在本例中,您的第一个参数是self
,因此您将有四个参数)我得到错误:如果D[child_node]def dijkstra(self, graph, start, end):
D = {} # Final distances dict
P = {} # Predecessor dict
for node in graph.keys():
D[node] = -1 # Vertices are unreachable
P[node] = ""
D[start] = 0 # The start vertex needs no move
unseen_nodes = graph.keys() # All nodes are unseen
while len(unseen_nodes) > 0:
shortest = None
node = ''
for temp_node in unseen_nodes:
if shortest == None:
shortest = D[temp_node]
node = temp_node
elif (D[temp_node] < shortest):
shortest = D[temp_node]
node = temp_node
unseen_nodes.remove(node)
for child_node, child_value in graph[node].items():
if D[child_node] < D[node] + child_value['weight']: # I changed the code here
D[child_node] = D[node] + child_value['weight'] # I changed the code here
P[child_node] = node
path = []
node = end
while not (node == start):
if path.count(node) == 0:
path.insert(0, node) # Insert the predecessor of the current node
node = P[node] # The current node becomes its predecessor
else:
break
path.insert(0, start) # Finally, insert the start vertex
return path