3

我在 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 函数中将所有节点设置为“已访问 = 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

我为这篇文章冗长而道歉,我只是对我无法掌握似乎是基本功能的东西感到沮丧

4

2 回答 2

2

您将方法传递nearestNeighbourtentativeDistance而不是方法的结果。

于 2011-03-11T21:10:49.197 回答
1

这是问题

tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

应该

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

查看错误,您会看到代码正在尝试迭代不可迭代的对象。for - in -这在 Python语法 中是隐含的。

您还可以考虑重命名变量名或函数名以避免混淆。无论哪种方式,这都是一个容易犯的错误。

于 2011-03-11T21:09:04.257 回答