코드에 문제가 발생했습니다. 시작 노드에서 노드까지의 거리를 계산할 수 없습니다. ,Python - Dijsktra 's Algorithm 거리 문제
1,2,3,4,5,6,7,8,9
1,2,3,4,5,6,7,8 : 나는 형식의 텍스트 파일이 9
이것은 그래프의 노드 거리를 나타냅니다. 불행히도, 몇 가지 다른 방법을 시도 함에도 불구하고 여전히 다양한 오류 메시지가 계속 발생합니다.
infinity = 1000000
invalid_node = -1
startNode = 0
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
def populateNodeTable():
nodeTable = []
index =0
f = open('route.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print nodeTable[index].previous
print nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
def tentativeDistance(currentNode, nodeTable):
nearestNeighbour = []
for currentNode in nodeTable:
# if Node[currentNode].distFromSource + currentDistance = startNode + currentNode
# currentDistance = currentNode.distFromSource + nodeTable.currentNode
currentNode.previous = currentNode
currentNode.length = currentDistance
currentNode.visited = True
currentNode +=1
nearestNeighbour.append(currentNode)
print nearestNeighbour
return nearestNeighbour
def shortestPath (nearestNeighbour)
shortestPath = []
f = open ('spf.txt', 'r')
f.close()
currentNode = startNode
if __name__ == "__main__":
populateNodeTable()
tentativeDistance(currentNode,populateNodeTable())
내 tentativeDistance 기능에서 '#'으로 시작하는 줄은 문제가되는 부분입니다. 그들은 나를 혼동스럽게하지만 웹상에서 다른 구현물을 보았습니다.
코드의 포맷팅은 ' 맞는 것 같습니다. 따라하기 힘듭니다. 들여 쓰기를 수정 해 주시겠습니까? – dappawit
들여 쓰기가 깨졌습니다. 여기에 붙여 넣기 전에 탭과 공백을 섞어 놓지 않았는지 확인하고 싶을 수도 있습니다. –
문제가있는 기능을 제외하고 편집되었습니다. 나를 위해 잘 작동합니다. – user612041