python-Dijkstra-最近邻居确定
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-Dijkstra-最近邻居确定,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3868字,纯文字阅读大概需要6分钟。
内容图文
![python-Dijkstra-最近邻居确定](/upload/InfoBanner/zyjiaocheng/653/03028372c190474a9c55c954450b5ea0.jpg)
在确定Dijkstra算法中最邻近的邻居时,我感到很困惑.我得到如下奇怪的结果
首先,这是我的网络文件的内容,代表7个节点之间的距离:
(不包括第一栏中的数字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#
>
另外,这很好-除了上述功能外,执行我的NearestNeighbour函数后的输出是:
[0, 1, 2, 3, 4, 5, 6]
此输出是错误的,这是我的问题开始的地方
同样,当我运行包括tentativeDistance在内的所有代码时,也会出现此错误:
> for nodeIndex in nearestNeighbour:
TypeError: 'function' object is not iterable
我为这篇博文冗长而道歉,我很沮丧,因为我无法掌握似乎是基本功能的内容
解决方法:
这是问题
tentativeDistance(theNetwork, nodeTable, nearestNeighbour)
应该
x = nearestNeighbour(nodeTable, theNetwork)
tentativeDistance(theNetwork, nodeTable, x)
查看错误,您将看到代码正在尝试遍历不可迭代的对象.这在Python中隐含-in-语法.
您也可以考虑重命名变量名或函数名,以避免混淆.这是很容易犯的错误.
内容总结
以上是互联网集市为您收集整理的python-Dijkstra-最近邻居确定全部内容,希望文章能够帮你解决python-Dijkstra-最近邻居确定所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。