Python 迪克斯特拉';最短路径

Python 迪克斯特拉';最短路径,python,Python,我对Python(自学)非常陌生,正在编写一些代码,并且已经尽可能多地阅读了(在这个网站和youtube上)来了解它,我对它为什么不适合我感到困惑 我已经生成了这本词典(可能不是最有效的,请让我知道如何改进,只做了几周): graphx=[] 所有位置=[] def图形(宽度、高度): 对于范围内的r(高度): 行=[] 对于范围内的c(宽度): t=(r,c) 行。追加(t) 所有位置附加(t) graphx.append(行) 图形(宽度、高度) ##构建所有节点及其权重的字典 加权网格={

我对Python(自学)非常陌生,正在编写一些代码,并且已经尽可能多地阅读了(在这个网站和youtube上)来了解它,我对它为什么不适合我感到困惑

我已经生成了这本词典(可能不是最有效的,请让我知道如何改进,只做了几周):

graphx=[]
所有位置=[]
def图形(宽度、高度):
对于范围内的r(高度):
行=[]
对于范围内的c(宽度):
t=(r,c)
行。追加(t)
所有位置附加(t)
graphx.append(行)
图形(宽度、高度)
##构建所有节点及其权重的字典
加权网格={}
对于graphx中的节点:
对于节点中的c:
n={}
s={}
e={}
w={}
如果(c[0]<高度)和(c[0]>0):
n[c[0]+1,c[1]=1
s[c[0]-1,c[1]=1
elif c[0]==0:
n[c[0]+1,c[1]=1
elif c[0]==高度:
s[c[0]-1,c[1]=1
如果c[1]<宽度且c[1]>0:
e[c[0],c[1]+1]=1
w[c[0],c[1]-1]=1
elif c[1]==0:
e[c[0],c[1]+1]=1
elif c[1]==高度:
w[c[0],c[1]-1]=1
温度={}
空白={}
如果n!=空白:
温度[c[0]+1,c[1]]=1
如果e!=空白:
温度[c[0],c[1]+1]=1
如果s!=空白:
温度[c[0]-1,c[1]=1
如果w!=空白:
温度[c[0],c[1]-1]=1
加权网格[c[0],c[1]]=温度
当我运行dijikstras时,使用元组作为开始和目标,我得到一个错误。这是我正在运行的dijkstras版本:

def dijkstra(graph, start, goal):
shortest_distance = {}  # records the current cost to reach that node.
track_predecessor = {}  # keeps track of the path that led to this node.
unseen_nodes = graph    # Iterate through the graph to check all nodes.
infinity = 99999        # Make it any large number,greater than possible path weights.
track_path = []               # gives us the trace-back path of the optimal route

for node in unseen_nodes:
    shortest_distance[node] = infinity
shortest_distance[start] = 0

while unseen_nodes:

    min_distance_node = None
    for node in unseen_nodes:
        if min_distance_node is None:
            min_distance_node = node
        elif shortest_distance[node] < shortest_distance[min_distance_node]:
            min_distance_node = node

    path_options = graph[min_distance_node].items()

    for child_node, weight in path_options:

        if weight + shortest_distance[min_distance_node] < shortest_distance[child_node]:
            shortest_distance[child_node] = weight + shortest_distance[min_distance_node]
            track_predecessor[child_node] = min_distance_node

    unseen_nodes.pop(min_distance_node)

current_node = goal
while current_node != start:
    try:
        track_path.insert(0, current_node)
        current_node = track_predecessor[current_node]
    except KeyError:
        break
track_path.insert(0, start)

if shortest_distance[goal] != infinity:
    pass
def dijkstra(图形、开始、目标):
最短_距离={}#记录到达该节点的当前成本。
track_Preference={}#跟踪指向此节点的路径。
unseen_nodes=图#遍历图以检查所有节点。
无穷大=99999#使其任意大,大于可能的路径权重。
track_path=[]为我们提供了最佳路线的回溯路径
对于不可见节点中的节点:
最短距离[节点]=无穷大
最短距离[起点]=0
不可见的_节点时:
最小距离节点=无
对于不可见节点中的节点:
如果“最小距离”节点为“无”:
最小距离节点=节点
elif最短距离[节点]<最短距离[最小距离]节点]:
最小距离节点=节点
路径选项=图形[min\u distance\u node]。项()
对于子节点,路径选项中的权重:
如果权重+最短距离[min\u distance\u node]<最短距离[child\u node]:
最短距离[子节点]=权重+最短距离[最小距离节点]
轨迹前导[子节点]=最小距离节点
看不见的节点.pop(最小距离节点)
当前节点=目标
当前_节点时!=开始:
尝试:
跟踪路径。插入(0,当前节点)
当前\u节点=跟踪\u前置[当前\u节点]
除KeyError外:
打破
轨道路径。插入(0,开始)
如果最短距离[目标]!=无穷:
通过
我得到的错误是:

Traceback (most recent call last):
  File "C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py", line 318, in <module>
    dijkstra(weighted_grid, (0, 0), (0,1))
  File "C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py", line 300, in dijkstra
    if weight + shortest_distance[min_distance_node] < shortest_distance[child_node]:
KeyError: (0, 30)
回溯(最近一次呼叫最后一次):
文件“C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py”,第318行,在
dijkstra(加权网格,(0,0,0,1))
文件“C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py”,第300行,dijkstra
如果权重+最短距离[min\u distance\u node]<最短距离[child\u node]:
KeyError:(0,30)

感谢您的帮助和建设性的批评。

我刚刚在谷歌上快速搜索了一下,发现了以下内容:。检查一下。另外,只要搜索一下Dijkstra的最短路径算法,你就会发现无数的例子。我已经看过了这两个例子,不管出于什么原因,错误总是说某些点不起作用,即使节点相邻\嗯,奇怪。嗯,等一下,我看看问题出在哪里。我刚刚在谷歌上快速搜索了一下,发现了这个:。检查一下。另外,只要搜索一下Dijkstra的最短路径算法,你就会发现无数的例子。我已经看过了这两个例子,不管出于什么原因,错误总是说某些点不起作用,即使节点相邻\嗯,奇怪。嗯,等一下,我看看问题出在哪里。
Traceback (most recent call last):
  File "C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py", line 318, in <module>
    dijkstra(weighted_grid, (0, 0), (0,1))
  File "C:/Users/Dave/Desktop/Important/PycharmProjects/DMT2/dungeonmasterstome/Main.py", line 300, in dijkstra
    if weight + shortest_distance[min_distance_node] < shortest_distance[child_node]:
KeyError: (0, 30)