开发者

Dijkstra - Nearest Neighbour Determination

I have come unstuck in my determination of nearest neighbours in Dijkstra's algorithm. I am getting strange results as follows Firstly this is the contents of my network file, representing the distances between the 7 nodes:

http://pastebin.com/PUM5qT6D

(The numbers 1-7 in the first column aren't included)

Now for my code:

> 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)

So, I am trying to look at the values provided by the network function, set all nodes to 'visited = false' in populateNodeTable function and then determine the nodes' nearest neighbour by looking at the values provided in the previous function, though I get this error message:

> 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

When I just run my network function, I get this ouput:

[[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]]

So far, so good - when I run my populateNodeTable function along with my network function I get this output:

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

Also, that's good - my ouput after executing my nearestNeighbour function in addition to the above functions is:

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

This output is wrong, and is where my problems start

Also when I run all of my code including tentativeDistance I get this error:

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

I apologise for this post being long winded, i'm just frustrated that I can't master what seems to be basic functionality


You're passing the method nearestNeighbour to tentativeDistance instead of the result of the method.


This is the issue

tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

should be

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

Taking a look at the error, you'll see the code is trying to iterate over a non-iterable object. This is implicit in the Python for - in - syntax.

You might also consider renaming your variable names or the function name to avoid confusion. This is an easy mistake to make either way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜