import csv
with open('cidades.csv', 'r') as f:
list2 = [tuple(line.values()) for line in csv.DictReader(f)]
def dfs_helper(list2, start_city, end_city):
pilha = []
visited = []
adj_cities = get_connections(list2, start_city)
dfs_visit(start_city,visited,pilha,adj_cities)
def dfs_visit(start_city,visited,pilha):
visited.append(start_city)
if(end_city in visited):
return pilha
for adj_city in start_city:
pilha.append(adj_city)
if adj_city not in visited:
dfs_visit(adj_city,visited,pilha)
def get_connections(list2, city,adj_city):
ligacoes = []
for result in list2:
if (result[0] == city):
ligacoes.insert(0, result[1])
elif (result[1] == city and result[0] != city):
ligacoes.insert(0, result[0])
ligacoes.reverse()
return ligacoes
dfs_helper(list2, "Lisboa", "Moscovo")
I need to find the smallest path between the two cities. The tuples are of the form (Origin, Destination, Distance).