我在确定每个节点距起始节点的距离时遇到了一些麻烦,或者更确切地说是取回任何信息。 我的函数没有输出,附在以下链接中。
#Values to assign to each node
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
#for each node assign default values
def populateNodeTable(network):
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print "The previous node is " ,nodeTable[index].previous
print "The distance from source is " ,nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
#calculate the distance of each node from the start node
def tentativeDistance(currentNode, nodeTable):
nearestNeighbour = []
for currentNode in nearestNeighbour:
currentDistance == currentNode.distFromSource + [currentNode][nearestNeighbour] #gets current distance from source
print "The current distance"
if currentDistance != 0 & currentNode.distFromSource < Node[currentNode].distFromSource:
nodeTable[currentNode].previous = currentNode
nodeTable[currentNode].length = currentDistance
nodeTable[currentNode].visited = True
nodeTable[currentNode] +=1
nearestNeighbour.append(currentNode)
for currentNode in nearestNeighbour:
print nearestNeighbour
return nearestNeighbour
至少在我看来,我的逻辑是正确的;但是,在运行代码时,我得到的错误信息并不多。
答案 0 :(得分:2)
你将nearestNeighbour
设置为一个空列表,然后你用for currentNode in nearestNeighbour
循环它 - 它什么都不做,因为列表是空的 - 然后你就是从函数返回。
(我假设tentativeDistance
是您正在调用的函数,并且什么都没有看到。)
答案 1 :(得分:1)
您应该重新考虑您的算法设计。尝试查找Dijkstra算法的伪代码定义并在Python中实现它。特别是,您应该考虑程序中的控制流程。
您可能希望查看this cookbook recipe以获取Dijkstra的Python实现,看看您是否能够理解它。