2011-03-11 70 views
3

我對Dijkstra算法中對最近鄰居的確定感到失落。我得到了奇怪的結果如下 首先,這是我的網絡文件的內容,代表7個節點之間的距離:Dijkstra - 最近鄰點確定

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功能,然後通過查看以前的功能提供的值決定了節點的近鄰,但我得到這個錯誤信息:

> 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 

我很抱歉這篇文章長時間囉嗦,我只是沮喪,我不能掌握什麼似乎是基本功能

回答

1

這是問題

tentativeDistance(theNetwork, nodeTable, nearestNeighbour) 

應該

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

考慮看看錯誤,你會看到代碼試圖迭代通過非迭代的對象。這隱含在Python for - in -語法中。

您可能還會考慮重命名變量名稱或函數名稱以避免混淆。這是一個很容易犯的錯誤。

2

您正在將方法nearestNeighbour傳遞給tentativeDistance而不是方法的結果。