Advertisement
macko9393

Untitled

Nov 11th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. INVALID_NODE = -1
  2. INFINITY = 1000000
  3.  
  4. class Node:
  5. previous = INVALID_NODE
  6. distFromSource = INFINITY
  7. visited = False
  8.  
  9.  
  10. def PopulateNetwork(filename):
  11. network = []
  12. networkfile = open(filename, "r")
  13. for line in networkfile:
  14. line = line.replace("\n","")
  15. line = line.split(',')
  16. line = map(int, line)
  17. network.append(line)
  18. return network
  19.  
  20.  
  21. def PopulateNodeTable(network, startNode):
  22. nodeTable = []
  23.  
  24. for node in network:
  25. nodeTable.append(Node())
  26.  
  27. nodeTable[startNode].distFromSource = 0
  28. nodeTable[startNode].visited = True
  29. return nodeTable
  30.  
  31.  
  32. def FindNearest(network, nodeTable, currentNode):
  33. nearestNeighbour = []
  34. columnIndex = 0
  35.  
  36. for entry in network[currentNode]:
  37. if entry != 0 and nodeTable[columnIndex].visited == False:
  38. nearestNeighbour.append(columnIndex)
  39. columnIndex += 1
  40. return nearestNeighbour
  41.  
  42. def CalculateTentative(nodeTable, network, currentNode, nearestNeighbours):
  43.  
  44. #calculate the distance from currentNode to each node in neighborous list
  45. #work out distance from source for each node
  46. #if lower than current entry in nodeTable for that node, update
  47.  
  48. for neighbour in nearestNeighbours:
  49. tentativeDistance = nodeTable[currentNode].distFromSource + network[currentNode][neighbour]
  50. if nodeTable[neighbour].distFromSource > tentativeDistance:
  51. nodeTable[neighbour].distFromSource = tentativeDistance
  52. nodeTable[neighbour].previous = currentNode
  53. return nodeTable
  54.  
  55. def FindNextNode(nodeTable, currentNode, destination):
  56. nodeTable[currentNode].visited = True
  57.  
  58. distance = 100000
  59. count = 0
  60.  
  61. if currentNode != destination:
  62. for entry in nodeTable:
  63. if entry.visited == False and entry.distFromSource < distance:
  64. distance = entry.distFromSource
  65. currentNode = count
  66. count = count + 1
  67.  
  68. else:
  69. print "This is your destination: "
  70. print currentNode
  71.  
  72.  
  73. return currentNode
  74.  
  75.  
  76.  
  77.  
  78. network = PopulateNetwork("network.txt")
  79. startNode = 1
  80. currentNode = startNode
  81. nodeTable = PopulateNodeTable(network, startNode)
  82.  
  83. print "Network file: \n"
  84. for line in network:
  85. print line
  86.  
  87. nearestNeighbour = FindNearest(network, nodeTable, currentNode)
  88.  
  89. print "\nNearest neighbour \n"
  90. for neighbour in nearestNeighbour:
  91. print neighbour
  92.  
  93. newDistance = CalculateTentative(nodeTable, network, currentNode, nearestNeighbour)
  94.  
  95. print "\nTentativce distance \n"
  96. for newNode in newDistance:
  97. print newNode.distFromSource
  98. print newNode.previous
  99. print newNode.visited
  100. print "\n"
  101.  
  102. destination = 3
  103.  
  104. currentNode = FindNextNode(nodeTable, currentNode, destination)
  105.  
  106. print currentNode
  107.  
  108. ##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement