Dijkstra - 最近邻确定

时间:2011-03-11 20:47:55

标签: python dijkstra

我在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#
> 

另外,这很好 - 除上述功能外,执行myNeighbour函数后的输出是:

  

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

此输出错误,是我的问题开始的地方

当我运行包括tentativeDistance在内的所有代码时,我收到此错误:

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

我为这篇文章长篇大论而道歉,我很沮丧,因为我无法掌握看似基本功能的内容

2 个答案:

答案 0 :(得分:2)

您正在将方法nearestNeighbour传递给tentativeDistance,而不是方法的结果。

答案 1 :(得分:1)

这就是问题

tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

应该是

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

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

您可能还会考虑重命名变量名称或函数名称以避免混淆。无论如何,这都是一个容易犯的错误。

相关问题