迪杰斯特拉 - 最近邻确定

3 投票
2 回答
1557 浏览
提问于 2025-04-16 13:32

我在使用Dijkstra算法找最近邻居的时候遇到了麻烦,结果有点奇怪。首先,这是我的网络文件内容,表示7个节点之间的距离:

http://pastebin.com/PUM5qT6D

(第一列的数字1到7不算在内)

接下来是我的代码:

> infinity = 1000000 invalid_node = -1
> startNode = 0
> 
> #Values to assign to each node class Node:
>      distFromSource = infinity
>      previous = invalid_node
>      visited = False
> 
> #read in all network nodes
> #node = the distance values between nodes def network():
>     f = open ('network.txt', 'r')
>     theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
>     #print theNetwork
> 
>     return theNetwork
> 
> #for each node assign default values
> #populate table with default values def populateNodeTable():
>     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
> 
> #find the nearest neighbour to a particular node def
> nearestNeighbour(nodeTable,
> theNetwork):
>      nearestNeighbour = []
>      nodeIndex = 0
>      for node in nodeTable:
>           if node != 0 and Node.visited == False:
>              nearestNeighbour.append(nodeIndex)
>              nodeIndex +=1
>      print nearestNeighbour
> 
>      return nearestNeighbour
> 
> def tentativeDistance (theNetwork,
> nodeTable, nearestNeighbour):
>     shortestPath = []
>     for nodeIndex in nearestNeighbour:
>          currentDistance = nearestNeighbour[] + startNode
>          print currentDistance
> ##         if currentDistance < Node.distFromSource:
> ##            theNetwork[Node].previous = nodeIndex
> ##            theNetwork[Node].length = nodeIndex
> ##            theNetwork[Node].visited = True;
> ##            shortestPath.append(indexNode)
> ##            nodeIndex +=1
> ##    print shortestPath
> 
> currentNode = startNode
> 
> if __name__ == "__main__":
>     nodeTable = populateNodeTable()
>     theNetwork = network()
>     nearestNeighbour(nodeTable, theNetwork)
>     tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

我想查看网络函数提供的值,在populateNodeTable函数中把所有节点的'visited'状态设为'false',然后通过查看之前函数提供的值来确定节点的最近邻居,但我收到了这个错误信息:

> Traceback (most recent call last):  
> File "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 77, in <module>
>     tentativeDistance(theNetwork, nodeTable, nearestNeighbour)   File
> "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 51, in tentativeDistance
>     for nodeIndex in nearestNeighbour: TypeError: 'function' object is not iterable

当我只运行我的网络函数时,得到的输出是:

[[0, 2, 4, 1, 6, 0, 0], [2, 0, 0, 0, 5, 0, 0], [4, 0, 0, 0, 5, 5, 0], [1, 0, 0, 0, 1, 1, 0], [6, 5, 0, 1, 0, 5, 5], [0, 0, 5, 1, 5, 0, 0], [0, 0, 0, 0, 5, 0, 0]]

到目前为止,一切正常 - 当我同时运行populateNodeTable函数和网络函数时,得到的输出是:

> The previous node is  -1 
  The distance from source is  1000000 # happens 7 times#
> 

这也很好 - 在执行nearestNeighbour函数后,我的输出是:

[0, 1, 2, 3, 4, 5, 6]

这个输出是错误的,这就是我问题的开始。

另外,当我运行所有代码,包括tentativeDistance时,我得到了这个错误:

> for nodeIndex in nearestNeighbour:
  TypeError: 'function' object is not iterable

抱歉这篇帖子有点长,我只是对无法掌握看起来很基础的功能感到沮丧。

2 个回答

2

你把方法 nearestNeighbour 传给了 tentativeDistance,而不是把方法的结果传过去。

1

这是问题所在

tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

应该是

 x = nearestNeighbour(nodeTable, theNetwork)
 tentativeDistance(theNetwork, nodeTable, x)

看看这个错误,你会发现代码试图对一个不能被“循环”的对象进行操作。这在Python的for - in -语法中是很明显的。

你也可以考虑重新命名你的变量或者函数名,以避免混淆。这种错误很容易发生。

撰写回答