Dijkstra's Shortest Path
dijkstras-shortest-path
import os
import time
cities = [
"Ab'Dendriel",
"Ankrahmun",
"Carlin",
"Darashia",
"Edron",
"Liberty Bay",
"Port Hope",
"Roshamuul",
"Oramond",
"Svargrond",
"Thais",
"Venore",
"Yalahar",
"Krailos",
"Issavi",
"Cormaya",
]
fares = [
[None, None, 80, None, 70, None, None, None, None, None, 130, 90, 160, None, None, None],
[None, None, None, 100, 160, 90, 80, None, None, None, None, 150, 230, None, None, None],
[ 80, None, None, None, 110, None, None, None, None, 110, 110, 130, 185, None, None, None],
[None, 100, None, None, None, 200, 180, None, None, None, None, 60, 210, 110, 130, None],
[ 70, 160, 110, None, None, 170, 150, None, None, None, 160, 40, None, 100, None, 20],
[None, 90, None, 200, 170, None, 50, None, None, None, 180, 180, 275, None, None, None],
[None, 110, None, 180, 150, 50, None, None, None, None, 160, 160, 260, None, None, None],
[None, None, None, None, None, None, None, None, None, None, 210, None, None, None, None, None],
[None, None, None, None, 110, None, 200, None, None, None, 150, 130, None, 60, 120, None],
[None, None, 110, None, None, None, None, None, None, None, 180, 150, None, None, None, None],
[ 130, None, 110, None, 160, 180, 160, 210, 150, 180, None, 170, 200, None, None, None],
[ 90, 150, 130, 60, 40, 180, 160, None, None, 150, 170, None, 185, 110, 130, None],
[ 160, 230, 185, 210, None, 275, 260, None, None, None, 200, 185, None, None, None, None],
[None, None, None, 110, 100, None, None, None, 60, None, None, 110, None, None, 70, None],
[None, None, None, 80, None, None, None, None, 100, None, None, 80, None, 80, None, None],
[None, None, None, None, 20, None, None, None, None, None, None, None, None, None, None, None],
]
def say(string):
time.sleep(0.2)
os.system('xdotool key Return')
os.system('xdotool keydown Alt')
os.system('xdotool type "' + string + '"')
os.system('xdotool keyup Alt')
os.system('xdotool key Return')
def typeFare(city):
say("hi")
time.sleep(1.0)
say(city)
say("yes")
def dijkstraShortestPath(sourceVertex):
explored = []
unexplored = []
for i, fare in enumerate(fares[sourceVertex]):
unexplored.append([i, None, None])
if i == sourceVertex:
unexplored[-1][1] = 0
while len(unexplored) > 0:
closest = None
distance = None
for i, vertex in enumerate(unexplored):
if (distance == None and vertex[1] != None) or \
(vertex[1] != None and vertex[1] < distance):
closest = i
distance = vertex[1]
explored.append(unexplored.pop(closest))
ID = explored[-1][0]
for i, vertex in enumerate(unexplored):
if fares[ID][vertex[0]] != None:
if vertex[1] == None:
vertex[1] = fares[ID][vertex[0]] + explored[-1][1]
vertex[2] = ID
elif vertex[1] > fares[ID][vertex[0]] + explored[-1][1]:
vertex[1] = fares[ID][vertex[0]] + explored[-1][1]
vertex[2] = ID
return explored
def dijkstraTraversePath(sourceVertex, targetVertex, explored):
find = targetVertex
path = [targetVertex]
while find != sourceVertex:
for i, vertex in enumerate(explored):
if vertex[0] == find:
find = vertex[2]
path.append(vertex[2])
break
path.pop()
path.reverse()
return path
def main():
locations = '\n'.join(cities)
current = os.popen('echo "' + locations + '" | rofi -dmenu -i -p "Where are you now?"').read().rstrip()
destination = os.popen('echo "' + locations + '" | rofi -dmenu -i -p "Where are you going?"').read().rstrip()
if not current or not destination: return
currentIndex = cities.index(current)
destinationIndex = cities.index(destination)
if fares[currentIndex][destinationIndex] != None:
typeFare(destination)
return
data = dijkstraShortestPath(currentIndex)
path = dijkstraTraversePath(currentIndex, destinationIndex, data)
for index in path:
typeFare(cities[index])
if __name__ == "__main__":
main()
Source:
https://www.youtube.com/watch?v=JcN_nq1EAr4