0

開始ノードから各ノードまでの距離を判断したり、情報をまったく取得したりするのに問題があります。次のリンクに添付されている関数から出力が得られません。

#Values to assign to each node
class Node:
     distFromSource = infinity
     previous = invalid_node
     visited = False

#for each node assign default values    
def populateNodeTable(network): 
    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

#calculate the distance of each node from the start node
def tentativeDistance(currentNode, nodeTable):
    nearestNeighbour = []
    for currentNode in nearestNeighbour:
      currentDistance == currentNode.distFromSource + [currentNode][nearestNeighbour] #gets current distance from source
      print "The current distance"
      if currentDistance != 0 & currentNode.distFromSource < Node[currentNode].distFromSource:
         nodeTable[currentNode].previous = currentNode
         nodeTable[currentNode].length = currentDistance
         nodeTable[currentNode].visited = True
         nodeTable[currentNode] +=1
         nearestNeighbour.append(currentNode)
         for currentNode in nearestNeighbour:
           print nearestNeighbour

    return nearestNeighbour

私の論理は、少なくとも私の考えでは正しいです。ただし、コードの実行時にエラー メッセージが表示されることはありません。

4

2 に答える 2

2

空のリストになるように設定nearestNeighbourしてから、リストが空であるため何もしないでループしfor currentNode in nearestNeighbour、関数から戻ります。

(私はtentativeDistanceあなたが呼び出して何も見ていない関数だと思います。)

于 2011-03-09T22:44:07.980 に答える
1

アルゴリズムの設計を再考する必要があります。ダイクストラのアルゴリズムの疑似コード定義を調べて、それを Python で実装してみてください。特に、プログラムの制御フローについて考える必要があります。

Dijkstra の Python 実装のこのクックブック レシピを見て、理解できるかどうかを確認してください。

于 2011-03-09T22:56:56.173 に答える