0
사용자 정의 그래프에 Dijkstras 알고리즘을 구현하려고 시도했습니다. 그러나 잘못된 솔루션을 제공합니다. 어쨌든 너희들이 내 모습을보고 도와 줄 수 있니?최단 경로가 올바른 soluton을 반환하지 않음
def ShortestPath(self, Source, Dest):
Distances = {}
Previous = {}
for EachNode in self.NodeList.keys():
Distances[EachNode] = -1
Previous[EachNode] = ""
Distances[Source] = 0
UnseenNodes = self.NodeList.keys()
while len(UnseenNodes) > 0:
ShortestDistance = None
Node = ""
for TempNode in UnseenNodes:
if ShortestDistance == None:
ShortestDistance = Distances[TempNode]
Node = TempNode
elif Distances[TempNode] < ShortestDistance:
ShortestDistance = Distances[TempNode]
Node = TempNode
UnseenNodes.remove(Node)
for Neighbour, NeighbourValue in self.NodeList[Node].Connections.items():
NeighbourID = Neighbour.GetID()
print NeighbourID
if Distances[NeighbourID] < Distances[Node] + NeighbourValue:
Distances[NeighbourID] = Distances[Node] + NeighbourValue
Previous[NeighbourID] = Node
print Previous
Path = []
Node = Dest
while not (Node == Source):
if Path.count(Node) == 0:
Path.insert(0,Node)
Node = Previous[Node]
else:
break
Path.insert(0,Source)
print Path
당신이 그것을 디버깅하려고 있나요? 예를 들어, Pycharm은 좋은 파이썬 디버거를 가지고 있으며, 각 단계에 중단 점을 넣고 더 많은 것을 조사 할 수 있습니다. – pagep