I decided on determining the nearest neighbors in Dijkstra's algorithm. I get strange results as follows Firstly, this is the contents of my network file, representing the distances between 7 nodes:
http://pastebin.com/PUM5qT6D
(numbers 1-7 in the first column are not included)
Now for my code:
> infinity = 1000000 invalid_node = -1
> startNode = 0
>
>
> distFromSource = infinity
> previous = invalid_node
> visited = False
>
>
>
> f = open ('network.txt', 'r')
> theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
>
>
> return theNetwork
>
>
>
> nodeTable = []
> index = 0
> f = open('network.txt', 'r')
> for line in f:
> node = map(int, line.split(','))
> nodeTable.append(Node())
>
>
>
> index +=1
> nodeTable[startNode].distFromSource =
> 0
>
> return nodeTable
>
>
> 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
>
>
>
>
>
>
>
>
> currentNode = startNode
>
> if __name__ == "__main__":
> nodeTable = populateNodeTable()
> theNetwork = network()
> nearestNeighbour(nodeTable, theNetwork)
> tentativeDistance(theNetwork, nodeTable, nearestNeighbour)
So, I'm trying to look at the values provided by the network function, set all the nodes to "visit = false" in the populateNodeTable function, and then determine the nearest neighboring node by looking at the values specified in the previous function, although 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 output:
[[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
>
, - nextNeighbour :
[0, 1, 2, 3, 4, 5, 6]
, , ,
, , tentativeDistance, :
> for nodeIndex in nearestNeighbour:
TypeError: 'function' object is not iterable
, , , ,