[英]Python won't run program
我是python的新手,但是具有C#和Matlab的大學經驗。 我正在嘗試編寫並運行dijkstras算法,但是當我運行它時,什么也沒發生。 我將附上代碼(包括注釋掉的失敗嘗試),以希望有人可以指導我朝正確的方向發展。
#must write script to turn matrix into this form of graph
graph = {'a':{'c':3,'e':5},'c':{'a':3,'d':5},'d':{'c':3,'e':6},'e':
{'d':6,'a':5}}
unseenNodes = {}
goal = {}
start = {}
#defining function
def dijkstra(graph, start, goal):
shortest_distance = {} #empty dictionary
predeccesor = {} #empty dictionary
unseenNodes = graph #makes it run through til all are seen
path = []
def __init__(self):
#start node to next node
print( str("hi"))
for node in unseenNodes:
shortest_distance[node]=9999999
shortest_distance[start]=0
print(shortest_distance)
#beefy bit of dijkstras alogrithim
while unseenNodes:
minNode=None
for node in unseenNodes:
if minNode is None:
minNode = node
elif shortest_distance[node] < shortest_distance[minNode]:
minNode = node
for childNode, weight in graph [minNode].items():
if weight + shortest_distance[minNode] <
shortest_distance[childNode]:
shortest_distance[childNode] = weight + shortest_distance[minNode]
predeccesor[childNode] = minNode
unseenNodes.pop(minNode)
print(shortest_distance)
#reverse stack approach to trace path
currentNode = goal
while currentNode != start:
try:
path.insert(0,currentNode)
currentNode = predeccesor[currentNode]
except KeyError:
print('path not valid')
#break
path.insert(0,start)
if shortest_distance[goal] != infinity:
#print(goal)
print('shortest distance is' + str(shortest_distance[goal]))
print('path is' + str(path))
else:
Print('Something Went Wrong!')
#break
dijkstra(graph, 'a', 'd')
dijkstra函數僅執行這四行代碼,因為它們是定義第二個函數之前的僅有的幾行代碼。 因此,當您最后調用此函數時,程序將創建一些空字典,然后關閉:
shortest_distance = {} #empty dictionary
predeccesor = {} #empty dictionary
unseenNodes = graph #makes it run through til all are seen
path = []
您定義的第二個函數init ()是一個類方法,您不能在類外部定義它。
首先查看python中的一些更基本的算法,並熟悉語法(我不知道它與C#有什么不同)。
好的,所以我修復了代碼的縮進,作用域和名稱問題(請參見下文),並且出現以下錯誤
hi
{'a': 0}
{'a': 0, 'c': 9999999}
{'a': 0, 'c': 9999999, 'd': 9999999}
{'a': 0, 'c': 9999999, 'd': 9999999, 'e': 9999999}
{'a': 0, 'c': 3, 'd': 9999999, 'e': 9999999}
Traceback (most recent call last):
File "dijkstras.py", line 69, in <module>
dijkstra(graph, 'a', 'd')
File "dijkstras.py", line 47, in dijkstra
unseen_nodes.pop(minNode)
KeyError: 'a'
也許這會幫助您繼續嗎?
#! /usr/bin/env python3
# dijkstras.py
import math
# must write script to turn matrix into this form of graph
graph = {
'a': {'c': 3, 'e': 5},
'c': {'a': 3, 'd': 5},
'd': {'c': 3, 'e': 6},
'e': {'d': 6, 'a': 5}
}
# defining function
def dijkstra(graph, start, goal):
shortest_distance = {} # empty dictionary
predeccesor = {} # empty dictionary
unseen_nodes = graph # makes it run through til all are seen
path = []
# start node to next node
print(str("hi"))
for node in unseen_nodes:
shortest_distance[node] = 9999999
shortest_distance[start] = 0
print(shortest_distance)
# beefy bit of dijkstras alogrithim
while unseen_nodes:
min_node = None
for node in unseen_nodes:
if min_node is None:
min_node = node
elif shortest_distance[node] < shortest_distance[min_node]:
min_node = node
for childNode, weight in graph[min_node].items():
if weight + shortest_distance[min_node] < shortest_distance[childNode]:
shortest_distance[childNode] = weight + shortest_distance[min_node]
predeccesor[childNode] = min_node
unseen_nodes.pop(min_node)
print(shortest_distance)
# reverse stack approach to trace path
current_node = goal
while current_node != start:
try:
path.insert(0, current_node)
current_node = predeccesor[current_node]
except KeyError:
print('path not valid')
# break
path.insert(0, start)
if shortest_distance[goal] != math.inf:
# print(goal)
print('shortest distance is' + str(shortest_distance[goal]))
print('path is' + str(path))
else:
print('Something Went Wrong!')
if __name__ == '__main__': # only run if script
dijkstra(graph, 'a', 'd')
您只需要一次更改
def __init__(self): function
。 __init__函數在類中用於初始化對象的變量。
您不能調用__init__
函數,這就是為什么您未獲得任何輸出的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.