Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import string
- import sys
- from heapq import heapify, heappush, heappop
- dirPath = os.path.dirname(os.path.realpath(__file__))
- strDataFileName = 'raw_data.txt'
- strTestDataFileName = 'test_data.txt'
- strTestData2FileName = 'test_data_2.txt'
- def fnReadConfig(path, filename):
- with open(os.path.join(path ,filename),"r") as raw_data_file:
- data = raw_data_file.read().split('\n')
- return data
- raw_data = fnReadConfig(dirPath,strTestDataFileName)
- aryMap = [list(x) for x in raw_data]
- intColumns = len(aryMap[0])
- intRows = len(aryMap)
- lstNodes = []
- lstAlphabet = list(string.ascii_lowercase)
- dctGraph = {}
- def fnGetNeighbours(row,column):
- global intColumns, intRows
- #lstNeighbours [north,east,south,west]
- lstNeighbours = []
- if row - 1 >= 0:
- lstNorthNeighbor = [row-1,column]
- lstNeighbours.append(lstNorthNeighbor)
- if column + 1 <= intColumns - 1 :
- lstEastNeighbor = [row,column+1]
- lstNeighbours.append(lstEastNeighbor)
- if row + 1 <= intRows - 1:
- lstSouthNeighbor = [row+1,column]
- lstNeighbours.append(lstSouthNeighbor)
- if column -1 >= 0:
- lstWestNeighbor = [row,column-1]
- lstNeighbours.append(lstWestNeighbor)
- return lstNeighbours
- def fnGetNeighbourDistances(lstNode,lstNeighbours):
- global aryMap, lstAlphabet, lstNodes
- dctNeighbours = {}
- strStartLetter = aryMap[lstNode[0]][lstNode[1]]
- if strStartLetter == 'S':
- intStartValue = -1
- elif strStartLetter == 'E':
- intStartValue = 26
- else:
- intStartValue = lstAlphabet.index(strStartLetter)
- for neighbour in lstNeighbours:
- intNeighbourNodeID = lstNodes.index(neighbour)
- strNeighbourLetter = aryMap[neighbour[0]][neighbour[1]]
- if strNeighbourLetter == 'E':
- intElevation = 26 - intStartValue
- elif strNeighbourLetter == 'S':
- intElevation = intStartValue + 1
- else:
- intElevation = lstAlphabet.index(strNeighbourLetter) - intStartValue
- if abs(intElevation) <= 1:
- dctNeighbours[intNeighbourNodeID] = 1
- return dctNeighbours
- def fnCreateNodeData():
- global lstNodes
- # create infinite value
- inf = sys.maxsize
- dctNodeData = {}
- for id in range(len(lstNodes)):
- dctNodeData[id] = {'cost':inf,'pred':[]}
- return dctNodeData
- def dijsktra(graph,src,dest):
- global lstNodes
- # create dictionary of dictionary showing cost and predecessor of each node in graph
- dctNodeData = fnCreateNodeData()
- # assign the cost of 0 for source node
- dctNodeData[src]['cost'] = 0
- # create set of visited nodes
- visited = []
- # maintain a temporary variable of the source variable which is the min of all the neighbours
- temp = src
- for i in range(len(lstNodes)-1):
- if temp not in visited:
- # markt that this node will now have been visited
- visited.append(temp)
- lstMinHeap = []
- # loop to traverse through the neighbours of the temp variable
- for j in graph[temp]:
- if j not in visited:
- cost = dctNodeData[temp]['cost'] + graph[temp][j]
- if cost < dctNodeData[j]['cost']:
- dctNodeData[j]['cost'] = cost
- dctNodeData[j]['pred'] = dctNodeData[temp]['pred'] + [temp]
- heappush(lstMinHeap,(dctNodeData[j]['cost'],j))
- heapify(lstMinHeap)
- temp = lstMinHeap[0][1]
- print(dctNodeData[1])
- print('Shortest Distance: ' + str(dctNodeData[dest]['cost']))
- print('Shortest Path: ' + str(dctNodeData[dest]['pred'] + [dest]))
- # Get ID and location of all nodes in map
- for row in range(intRows):
- for column in range(intColumns):
- lstNode = [row,column]
- lstNodes.append(lstNode)
- # Populate graph dictionary by finding neighbours of each node and the distance of these neighbours
- for row in range(intRows):
- for column in range(intColumns):
- # Empty dictionary for distances of each neighbour
- dctNeighbourDistances = {}
- # Location of node to be assessed
- lstNode = [row,column]
- if aryMap[row][column] == 'S':
- intSourceID = lstNodes.index(lstNode)
- elif aryMap[row][column] == 'E':
- intDestinationID = lstNodes.index(lstNode)
- intNodeID = lstNodes.index(lstNode)
- # location of all neighbours of node being assessed
- lstNeighbours = fnGetNeighbours(row,column)
- # populate distances of all neighbour nodes of node being assessed
- dctNeighbourDistances = fnGetNeighbourDistances(lstNode,lstNeighbours)
- # Append node details to graph dictionary
- dctGraph[intNodeID] = dctNeighbourDistances
- dijsktra(dctGraph,intSourceID,intDestinationID)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement